Skip to content

Commit 33136ba

Browse files
committed
2 parents 34266d1 + 2a0db89 commit 33136ba

80 files changed

Lines changed: 3520 additions & 209 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/python
2+
3+
"""
4+
a really simple module to use to test reloading
5+
"""
6+
7+
this = "this2"
8+
that = "that"
9+
10+
def print_something():
11+
print "I'm printing something else"
12+

‎Examples/Session03/test_script.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python
2+
3+
print "yes, it ran"
4+
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 form teh codingbat site:
5+
6+
http://codingbat.com/python
7+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
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/Session01/print_grid.py‎

100644100755
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"""
44
Chris' solution to the week 1 homework problem.
55
6-
Note that we hadn't talked about loops yet, so this is a solution with no
7-
loops.
6+
Note that we only did the basics of loops, and you can
7+
do all this without any loops at all, so that's what I did.
88
9-
Note also that there is more than one way to skin a cat -- or code a function
9+
Note also that there is more than one way to skin a cat --
10+
or code a function
1011
"""
1112

1213

0 commit comments

Comments
 (0)