Skip to content

Commit 8a09de6

Browse files
committed
more codingbat solutions
1 parent ca3b09d commit 8a09de6

5 files changed

Lines changed: 139 additions & 0 deletions

File tree

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: 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"

‎Solutions/Session01/codingbat/Warmup-1/pos_neg.py‎

100644100755
File mode changed.
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

0 commit comments

Comments
 (0)