Skip to content

Commit da31bbb

Browse files
committed
added solutions for session 1 and 2 excercises
1 parent bf07120 commit da31bbb

13 files changed

Lines changed: 603 additions & 0 deletions

File tree

‎solutions/Session01/break_me.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
A simple set of examples that raise various Exceptions
5+
"""
6+
7+
8+
def name_error():
9+
"""This function raises a NameError"""
10+
# very easy to do -- simply try to use a name you haven't defined
11+
x = something
12+
13+
14+
def type_error():
15+
"""This function raises a TypeError"""
16+
# Try to use an object in a way that doesn't make sense
17+
"543" / 3
18+
19+
def attribute_error():
20+
"""This function raises an AttributeError"""
21+
x = 5
22+
y = x.strip()
23+
24+
# have to comment this out, because the SyntaxError keeps the code from
25+
# running at all
26+
# def syntax_error():
27+
# """This function raises a SyntaxError"""
28+
# del = 32 # this one is tricky -- it's an error because "del" is a keyword
29+
30+
# now run the functions:
31+
# Note: I have all but one commented out, becaue the code stops running
32+
# when the first Error is hit.
33+
34+
# name_error()
35+
# type_error()
36+
attribute_error()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
3+
4+
def cigar_party(cigars, is_weekend):
5+
"""
6+
basic solution
7+
"""
8+
if is_weekend and cigars >= 40:
9+
return True
10+
elif 40 <= cigars <= 60:
11+
return True
12+
return False
13+
14+
15+
def cigar_party2(cigars, is_weekend):
16+
"""
17+
some direct return of bool result
18+
"""
19+
if is_weekend:
20+
return (cigars >= 40)
21+
else:
22+
return (cigars >= 40 and cigars <= 60)
23+
24+
25+
def cigar_party3(cigars, is_weekend):
26+
"""
27+
conditional expression
28+
"""
29+
return (cigars >= 40) if is_weekend else (cigars >= 40 and cigars <= 60)
30+
31+
if __name__ == "__main__":
32+
# some tests
33+
34+
assert cigar_party(30, False) is False
35+
assert cigar_party(50, False) is True
36+
assert cigar_party(70, True) is True
37+
assert cigar_party(30, True) is False
38+
assert cigar_party(50, True) is True
39+
assert cigar_party(60, False) is True
40+
assert cigar_party(61, False) is False
41+
assert cigar_party(40, False) is True
42+
assert cigar_party(39, False) is False
43+
assert cigar_party(40, True) is True
44+
assert cigar_party(39, True) is False
45+
46+
print("All tests passed")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CodingBat Solutions
2+
====================
3+
4+
A few selected solutions from the codingbat site:
5+
6+
http://codingbat.com/python
7+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
3+
4+
def diff21(n):
5+
"""
6+
basic solution
7+
"""
8+
if n > 21:
9+
return 2 * (n - 21)
10+
else:
11+
return 21 - n
12+
13+
14+
def diff21b(n):
15+
"""
16+
direct return of conditional expression
17+
"""
18+
return 2 * (n - 21) if n > 21 else 21 - n
19+
20+
if __name__ == "__main__":
21+
# needs
22+
print(diff21(3))
23+
print(diff21b(3))
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
3+
#############################
4+
# Warmup-1 > monkey_trouble
5+
6+
7+
def monkey_trouble(a_smile, b_smile):
8+
"""
9+
really simple solution
10+
"""
11+
if a_smile and b_smile:
12+
return True
13+
elif not a_smile and not b_smile:
14+
return True
15+
else:
16+
return False
17+
18+
19+
def monkey_trouble2(a_smile, b_smile):
20+
"""
21+
slightly more sophisticated
22+
"""
23+
if a_smile and b_smile or not (a_smile or b_smile):
24+
return True
25+
else:
26+
return False
27+
28+
29+
def monkey_trouble3(a_smile, b_smile):
30+
"""
31+
conditional expression -- kind of ugly in this case
32+
"""
33+
result = True if (a_smile and b_smile or not (a_smile or b_smile)) else False
34+
return result
35+
36+
37+
def monkey_trouble4(a_smile, b_smile):
38+
"""
39+
direct return of boolean result
40+
"""
41+
return a_smile and b_smile or not (a_smile or b_smile)
42+
43+
if __name__ == "__main__":
44+
# a few tests
45+
46+
# neat trick to test all versions:
47+
for test_fun in (monkey_trouble,
48+
monkey_trouble2,
49+
monkey_trouble3,
50+
monkey_trouble4):
51+
assert test_fun(True, True) is True
52+
assert test_fun(False, False) is True
53+
assert test_fun(True, False) is False
54+
assert test_fun(False, True) is False
55+
56+
print("All tests passed")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python
2+
3+
4+
def not_string(st):
5+
if st.startswith('not'):
6+
return st
7+
else:
8+
return 'not ' + st
9+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
3+
4+
def pos_neg(a, b, negative):
5+
if negative:
6+
return a < 0 and b < 0
7+
else:
8+
return (a < 0 and b > 0) or (a > 0 and b < 0)
9+
10+
if __name__ == "__main__":
11+
# run some tests if run as script
12+
# (from the codingbat site -- not all, I got bored)
13+
assert pos_neg(1, -1, False) is True
14+
assert pos_neg(-1, 1, False) is True
15+
assert pos_neg(-4, -5, True) is True
16+
assert pos_neg(-4, -5, False) is False
17+
assert pos_neg(-4, -5, True) is True
18+
19+
assert pos_neg(-6, -6, False) is False
20+
assert pos_neg(-2, -1, False) is False
21+
assert pos_neg(1, 2, False) is False
22+
assert pos_neg(-5, 6, True) is False
23+
assert pos_neg(-5, -5, True) is True
24+
25+
26+
27+
print "all tests passed"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
3+
4+
def sleep_in(weekday, vacation):
5+
"""
6+
basic solution
7+
"""
8+
if (not weekday) or vacation:
9+
return True
10+
else:
11+
return False
12+
13+
14+
def sleep_in2(weekday, vacation):
15+
"""
16+
direct return of boolean result
17+
"""
18+
return (not weekday) or vacation

‎solutions/Session02/fizz_buzz.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Fizz Buzz examples -- from most straightforward, to most compact.
5+
"""
6+
7+
8+
# basic approach:
9+
def fizzbuzz1(n):
10+
for i in range(1, n + 1):
11+
if i % 3 == 0 and i % 5 == 0:
12+
print("FizzBuzz")
13+
elif i % 3 == 0:
14+
print ("Fizz")
15+
elif i % 5 == 0:
16+
print ("Buzz")
17+
else:
18+
print(i)
19+
20+
21+
def fizzbuzz2(n):
22+
"""
23+
Why evaluate i%3 and i%5 twice?
24+
"""
25+
for i in range(1, n + 1):
26+
msg = ''
27+
if i % 3 == 0:
28+
msg += "Fizz"
29+
if i % 5 == 0:
30+
msg += "Buzz"
31+
if msg:
32+
print(msg)
33+
else:
34+
print(i)
35+
36+
37+
def fizzbuzz3(n):
38+
"""
39+
Or print on one line...
40+
"""
41+
for i in range(1, n + 1):
42+
num = True
43+
if i % 3 == 0:
44+
print("\nFizz", end='')
45+
num = False
46+
if i % 5 == 0:
47+
if num:
48+
print()
49+
print("Buzz", end='')
50+
num = False
51+
else:
52+
if num:
53+
print("\n", i, end='')
54+
55+
56+
def fizzbuzz4(n):
57+
"""
58+
use conditional expressions:
59+
"""
60+
for i in range(1, n + 1):
61+
msg = "Fizz" if i % 3 == 0 else ''
62+
msg += "Buzz" if i % 5 == 0 else ''
63+
print(msg or i)
64+
65+
66+
def fizzbuzz5(n):
67+
"""
68+
a one liner
69+
"""
70+
for i in range(1, n + 1): print (("Fizz" * (not (i % 3)) + "Buzz" * (not (i % 5))) or i)
71+
72+
73+
if __name__ == "__main__":
74+
fizzbuzz1(16)
75+
print()
76+
fizzbuzz2(16)
77+
print()
78+
fizzbuzz3(16)
79+
print()
80+
fizzbuzz4(16)
81+
print()
82+
fizzbuzz4(16)
83+
print()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python3
2+
3+
# This One Liner solution to the Fizz Buzz problem
4+
# was found by a student on the internet
5+
6+
for i in range(1,101): print([i,'Fizz','Buzz','FizzBuzz'][(i%3==0)+2*(i%5==0)])
7+
8+
# this is a good example of why the most compact code is not always the
9+
# best -- readability counts!
10+
# And this is pretty impenatrable.
11+
# but it's also pretty nifty logic, so below,
12+
# It's unpacked to make it easeir to understand.
13+
14+
# first, add some white space to make it pep8 compatible, and more readable.
15+
16+
for i in range(1, 101): print([i, 'Fizz', 'Buzz', 'FizzBuzz'][(i % 3 == 0) + 2 * (i % 5 == 0)])
17+
18+
# second, take the for loop off one line -- that really makes no difference:
19+
20+
for i in range(1, 101):
21+
print([i, 'Fizz', 'Buzz', 'FizzBuzz'][(i % 3 == 0) + 2 * (i % 5 == 0)])
22+
23+
# so we are looping through the numbers, and the contents of the print()
24+
# is deciding what to print for i
25+
26+
# unpack that line:
27+
28+
for i in range(1, 101):
29+
options_to_print = [i, 'Fizz', 'Buzz', 'FizzBuzz']
30+
index = 0 # default index is zero
31+
index += (i % 3 == 0) # add one to index if it's a multiple of 3
32+
index += (2 * (i % 5 == 0)) # add two to the index if a multiple of 5
33+
print(options_to_print[index]) # print the selection.
34+
35+
# there are 4 possible options that might get printed on each line:
36+
# 1) the number, i
37+
# 2) Fizz
38+
# 3) Buzz
39+
# 4) FizzBuzz
40+
41+
# we now need an index to pick which of these to print
42+
#
43+
# remember that True and False are also the integers 1 and 0:
44+
# so (i % 3 == 0) will return 1 (True) if i is a multiple of 3
45+
# and 0 (False) if not
46+
# and (i % 5 == 0) will do the same for 5
47+
# so (2 * (i % 5 == 0)) will return either 0 or 2
48+
#
49+
# so if the number is a multiple of neither, index will be zero,
50+
# and we'll get the zeroth element of the list: i
51+
#
52+
# if i is a multiple of three (i % 3 == 0), then the index will be 1
53+
#
54+
# and if i is a multiple of 5 the index will then be 3 (1+2)
55+
# if it wasn't a multiple of three, then it will be 2 (0+2)
56+
#
57+
# so using the index will get the right element from the options list.
58+
#
59+
# pretty slick!

0 commit comments

Comments
 (0)