Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
fa4d087
forked repo and submitting previous work
dcastrowa Jan 20, 2019
e340325
adding readme file
dcastrowa Jan 20, 2019
3b20123
adding a new file
dcastrowa Jan 20, 2019
5c92bcc
assignment 2 submission
dcastrowa Jan 23, 2019
476a53f
Add slicing file
dcastrowa Jan 26, 2019
36522ef
Add slicing functions
dcastrowa Jan 26, 2019
d3d9c05
Add new list lab file and series 1
dcastrowa Jan 26, 2019
20f02a6
Add series 2 and some of series 3
dcastrowa Jan 27, 2019
558eef4
Add series 3 and complete file
dcastrowa Jan 29, 2019
c2bec76
Complete series 4
dcastrowa Jan 29, 2019
b145179
Complete series 4 final
dcastrowa Jan 29, 2019
42bc382
Complete string format lab
dcastrowa Jan 29, 2019
d1bf4fb
Create file and thank_yu function
dcastrowa Jan 29, 2019
74cf137
Complete mailroom assignment
dcastrowa Jan 30, 2019
3bd6e43
complete and submit assignment 3
KevCav91 Jan 31, 2019
8969ea3
Add files via upload
rootrUW Jan 31, 2019
7026050
Delete mailroom.py
rootrUW Jan 31, 2019
6eb39ca
Delete mailroom2.py
rootrUW Jan 31, 2019
1198682
Add files via upload
rootrUW Jan 31, 2019
fcf6fd5
Add files via upload
rootrUW Jan 31, 2019
5a40be2
Update 00-README.md
rootrUW Jan 31, 2019
d845e8e
Update 00-README.md
rootrUW Jan 31, 2019
6276ed9
Add dictionary 1 and dictionary 2
dcastrowa Feb 3, 2019
3a7db66
Complete dictionary lab
dcastrowa Feb 3, 2019
e8fd619
Create file lab
dcastrowa Feb 3, 2019
1561c47
Complete trigrams file
dcastrowa Feb 3, 2019
506680e
Complete mail room part 2
dcastrowa Feb 3, 2019
2d2e02f
Add files needed
dcastrowa Feb 3, 2019
258a9a5
complete & submit assignment 4
KevCav91 Feb 5, 2019
7376608
cleaning up my github and folders and submitting weeks 1-4
KevCav91 Feb 5, 2019
9313d02
Merge pull request #12 from KevCav91/master
rootrUW Feb 7, 2019
0e9b17b
reorganize homework repos and complete exception ex
dcastrowa Feb 9, 2019
6c33859
Adjsut error eexception for mailroom part 3
dcastrowa Feb 10, 2019
1dc5566
Revise mail room part 3
dcastrowa Feb 10, 2019
d08b16c
Merge branch 'master' of https://github.com/rootrUW/Python210-W19
dcastrowa Feb 14, 2019
790c4f0
Adjust get_report() and create display_report()
dcastrowa Feb 16, 2019
a0da59b
Create list_of_donors()
dcastrowa Feb 16, 2019
58634b3
Create test_add_donation() to test_mailroom.py
dcastrowa Feb 16, 2019
1d44582
Complete and update mailroom part 4 and mailroom test
dcastrowa Feb 17, 2019
1d6f5e1
Update folders
dcastrowa Feb 18, 2019
7237b76
Complete step 1 of tutorial
dcastrowa Feb 23, 2019
fba6086
Start Step 3 of tutorial
dcastrowa Feb 24, 2019
a444d0d
Add one line tag subclass and title subclass
dcastrowa Feb 24, 2019
492758b
Comlete step 3 run_html and OneLineTag.append not implemented err
dcastrowa Feb 26, 2019
2c93603
Complete html render assignement
dcastrowa Feb 27, 2019
676e58a
Add runnable scripts
dcastrowa Mar 10, 2019
bae4447
Compelete final project
dcastrowa Mar 15, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions solutions/00-README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Solutions
Place solutions in this directory as they become available.
You can review these solutions as they become available and compare them to your own code. They are provided for comparison and do not directly determine your grade, but if your code is close to these examples, you are doing fine!

- Please remember that these are just one way to do the exercies and may not even the best way.


Remember to document your solutions, preferably mentioning where
students might get tripped up. None of the teachers, and none of the students,
want to debug *your* code.
36 changes: 36 additions & 0 deletions solutions/Lesson01/break_me.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python

"""
A simple set of examples that raise various Exceptions
"""


def name_error():
"""This function raises a NameError"""
# very easy to do -- simply try to use a name you haven't defined
x = something


def type_error():
"""This function raises a TypeError"""
# Try to use an object in a way that doesn't make sense
"543" / 3

def attribute_error():
"""This function raises an AttributeError"""
x = 5
y = x.strip()

# have to comment this out, because the SyntaxError keeps the code from
# running at all
# def syntax_error():
# """This function raises a SyntaxError"""
# del = 32 # this one is tricky -- it's an error because "del" is a keyword

# now run the functions:
# Note: I have all but one commented out, becaue the code stops running
# when the first Error is hit.

# name_error()
# type_error()
attribute_error()
45 changes: 45 additions & 0 deletions solutions/Lesson01/codingbat/Logic-1/cigar_party.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python


def cigar_party(cigars, is_weekend):
"""
basic solution
"""
if is_weekend and cigars >= 40:
return True
elif 40 <= cigars <= 60:
return True
return False


def cigar_party2(cigars, is_weekend):
"""
some direct return of bool result
"""
if is_weekend:
return (cigars >= 40)
return (cigars >= 40 and cigars <= 60)


def cigar_party3(cigars, is_weekend):
"""
conditional expression
"""
return (cigars >= 40) if is_weekend else (cigars >= 40 and cigars <= 60)

if __name__ == "__main__":
# some tests

assert cigar_party(30, False) is False
assert cigar_party(50, False) is True
assert cigar_party(70, True) is True
assert cigar_party(30, True) is False
assert cigar_party(50, True) is True
assert cigar_party(60, False) is True
assert cigar_party(61, False) is False
assert cigar_party(40, False) is True
assert cigar_party(39, False) is False
assert cigar_party(40, True) is True
assert cigar_party(39, True) is False

print("All tests passed")
7 changes: 7 additions & 0 deletions solutions/Lesson01/codingbat/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CodingBat Solutions
====================

A few selected solutions from the codingbat site:

http://codingbat.com/python

23 changes: 23 additions & 0 deletions solutions/Lesson01/codingbat/Warmup-1/diff21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python


def diff21(n):
"""
basic solution
"""
if n > 21:
return 2 * (n - 21)
else:
return 21 - n


def diff21b(n):
"""
direct return of conditional expression
"""
return 2 * (n - 21) if n > 21 else 21 - n

if __name__ == "__main__":
# needs
print(diff21(3))
print(diff21b(3))
56 changes: 56 additions & 0 deletions solutions/Lesson01/codingbat/Warmup-1/monkey_trouble.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python

#############################
# Warmup-1 > monkey_trouble


def monkey_trouble(a_smile, b_smile):
"""
really simple solution
"""
if a_smile and b_smile:
return True
elif not a_smile and not b_smile:
return True
else:
return False


def monkey_trouble2(a_smile, b_smile):
"""
slightly more sophisticated
"""
if a_smile and b_smile or not (a_smile or b_smile):
return True
else:
return False


def monkey_trouble3(a_smile, b_smile):
"""
conditional expression -- kind of ugly in this case
"""
result = True if (a_smile and b_smile or not (a_smile or b_smile)) else False
return result


def monkey_trouble4(a_smile, b_smile):
"""
direct return of boolean result
"""
return a_smile and b_smile or not (a_smile or b_smile)

if __name__ == "__main__":
# a few tests

# neat trick to test all versions:
for test_fun in (monkey_trouble,
monkey_trouble2,
monkey_trouble3,
monkey_trouble4):
assert test_fun(True, True) is True
assert test_fun(False, False) is True
assert test_fun(True, False) is False
assert test_fun(False, True) is False

print("All tests passed")
9 changes: 9 additions & 0 deletions solutions/Lesson01/codingbat/Warmup-1/not_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python


def not_string(st):
if st.startswith('not'):
return st
else:
return 'not ' + st

27 changes: 27 additions & 0 deletions solutions/Lesson01/codingbat/Warmup-1/pos_neg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python


def pos_neg(a, b, negative):
if negative:
return a < 0 and b < 0
else:
return (a < 0 and b > 0) or (a > 0 and b < 0)

if __name__ == "__main__":
# run some tests if run as script
# (from the codingbat site -- not all, I got bored)
assert pos_neg(1, -1, False) is True
assert pos_neg(-1, 1, False) is True
assert pos_neg(-4, -5, True) is True
assert pos_neg(-4, -5, False) is False
assert pos_neg(-4, -5, True) is True

assert pos_neg(-6, -6, False) is False
assert pos_neg(-2, -1, False) is False
assert pos_neg(1, 2, False) is False
assert pos_neg(-5, 6, True) is False
assert pos_neg(-5, -5, True) is True



print "all tests passed"
18 changes: 18 additions & 0 deletions solutions/Lesson01/codingbat/Warmup-1/sleep_in.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python


def sleep_in(weekday, vacation):
"""
basic solution
"""
if (not weekday) or vacation:
return True
else:
return False


def sleep_in2(weekday, vacation):
"""
direct return of boolean result
"""
return (not weekday) or vacation
78 changes: 78 additions & 0 deletions solutions/Lesson02/fizz_buzz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python

"""
Fizz Buzz examples -- from most straightforward, to most compact.
"""


# basic approach:
def fizzbuzz1(n):
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)


def fizzbuzz2(n):
"""
Why evaluate i%3 and i%5 twice?
"""
for i in range(1, n + 1):
msg = ''
if i % 3 == 0:
msg += "Fizz"
if i % 5 == 0:
msg += "Buzz"
if msg:
print(msg)
else:
print(i)


def fizzbuzz3(n):
"""
Or print on one line...
"""
for i in range(1, n + 1):
if i % 3 == 0:
print("Fizz", end="")
if i % 5 == 0:
print("Buzz", end="")
elif i % 3: # have to somehow check if you need to print the number
print(i, end="")
print()


def fizzbuzz4(n):
"""
use conditional expressions:
"""
for i in range(1, n + 1):
msg = "Fizz" if i % 3 == 0 else ''
msg += "Buzz" if i % 5 == 0 else ''
print(msg or i)


def fizzbuzz5(n):
"""
a one liner
"""
for i in range(1, n + 1): print (("Fizz" * (not (i % 3)) + "Buzz" * (not (i % 5))) or i)


if __name__ == "__main__":
fizzbuzz1(16)
print()
fizzbuzz2(16)
print()
fizzbuzz3(16)
print()
fizzbuzz4(16)
print()
fizzbuzz4(16)
print()
59 changes: 59 additions & 0 deletions solutions/Lesson02/fizz_buzz_one_liner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python3

# This One Liner solution to the Fizz Buzz problem
# was found by a student on the internet

for i in range(1,101): print([i,'Fizz','Buzz','FizzBuzz'][(i%3==0)+2*(i%5==0)])

# this is a good example of why the most compact code is not always the
# best -- readability counts!
# And this is pretty impenatrable.
# but it's also pretty nifty logic, so below,
# It's unpacked to make it easeir to understand.

# first, add some white space to make it pep8 compatible, and more readable.

for i in range(1, 101): print([i, 'Fizz', 'Buzz', 'FizzBuzz'][(i % 3 == 0) + 2 * (i % 5 == 0)])

# second, take the for loop off one line -- that really makes no difference:

for i in range(1, 101):
print([i, 'Fizz', 'Buzz', 'FizzBuzz'][(i % 3 == 0) + 2 * (i % 5 == 0)])

# so we are looping through the numbers, and the contents of the print()
# is deciding what to print for i

# unpack that line:

for i in range(1, 101):
options_to_print = [i, 'Fizz', 'Buzz', 'FizzBuzz']
index = 0 # default index is zero
index += (i % 3 == 0) # add one to index if it's a multiple of 3
index += (2 * (i % 5 == 0)) # add two to the index if a multiple of 5
print(options_to_print[index]) # print the selection.

# there are 4 possible options that might get printed on each line:
# 1) the number, i
# 2) Fizz
# 3) Buzz
# 4) FizzBuzz

# we now need an index to pick which of these to print
#
# remember that True and False are also the integers 1 and 0:
# so (i % 3 == 0) will return 1 (True) if i is a multiple of 3
# and 0 (False) if not
# and (i % 5 == 0) will do the same for 5
# so (2 * (i % 5 == 0)) will return either 0 or 2
#
# so if the number is a multiple of neither, index will be zero,
# and we'll get the zeroth element of the list: i
#
# if i is a multiple of three (i % 3 == 0), then the index will be 1
#
# and if i is a multiple of 5 the index will then be 3 (1+2)
# if it wasn't a multiple of three, then it will be 2 (0+2)
#
# so using the index will get the right element from the options list.
#
# pretty slick!
Loading