Skip to content

Commit b00e301

Browse files
committed
added Session02 solutions: ack, series, fizz_buzz
1 parent 41da098 commit b00e301

3 files changed

Lines changed: 224 additions & 0 deletions

File tree

‎Solutions/Session02/ack.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
"""
3+
Solution to the Ackerman function problem.
4+
5+
The Ackerman function is recusively defines as:
6+
7+
A(m, n) =
8+
n+1 if m = 0
9+
A(m-1, 1) if m > 0 and n = 0
10+
A(m-1, A(m, n-1)) if m > 0 and n > 0
11+
See http://en.wikipedia.org/wiki/Ackermann_function.
12+
"""
13+
14+
15+
def ack(m, n):
16+
"""Compute the Ackerman function"""
17+
if m < 0 or n < 0:
18+
return None
19+
if m == 0:
20+
return n+1
21+
elif n == 0:
22+
return ack(m-1, 1)
23+
else:
24+
return ack(m-1, ack(m, n-1))
25+
26+
if __name__ == "__main__":
27+
28+
# tests from the table in wikipedia
29+
# you don't really need to test them all
30+
# they will get called as part of the recusive calls anyway...
31+
32+
assert ack(0,0) == 1
33+
assert ack(0,4) == 5
34+
35+
assert ack(1,2) == 4
36+
assert ack(1,4) == 6
37+
38+
assert ack(2,2) == 7
39+
assert ack(2,4) == 11
40+
41+
assert ack(3,2) == 29
42+
assert ack(3,4) == 125
43+
44+
## ack(4,*) exceeds the recursion limit.
45+
46+
# check for neagative inputs -- should return None.
47+
assert ack(-1, 0) is None
48+
assert ack(2, -1) is None
49+
50+
51+
print "tests passed"

‎Solutions/Session02/fizz_buzz.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
use conditional expressions:
40+
"""
41+
for i in range(1, n+1):
42+
msg = "Fizz" if i%3 == 0 else ''
43+
msg += "Buzz" if i%5 == 0 else ''
44+
print msg or i
45+
46+
47+
def fizzbuzz4(n):
48+
"""
49+
the one liner
50+
"""
51+
for i in range(1,n+1): print ( "Fizz" * (not (i%3)) + "Buzz" * (not (i%5)) ) or i
52+
53+
54+
def fizzbuzz_ruby(n):
55+
"""
56+
This is a one-liner version inspired by the Ruby one-liner
57+
found here:
58+
59+
http://www.commandercoriander.net/blog/2013/02/03/fizzbuzz-in-one-line
60+
61+
This uses list comprehensions, and slicing, and is, well, pretty darn ugly!
62+
63+
"""
64+
for word in [ ("".join(["Fizz",][0:1-i%3]+["Buzz",][0:1-i%5]) or `i`) for i in range(1, n+1)]: print word
65+
66+
if __name__ == "__main__":
67+
fizzbuzz1(16)
68+
print
69+
fizzbuzz2(16)
70+
print
71+
fizzbuzz3(16)
72+
print
73+
fizzbuzz4(16)
74+
print
75+
fizzbuzz_ruby(16)
76+
77+

‎Solutions/Session02/series.py‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
series.py
5+
6+
solutions to the Fibonacci Series and Lucas numbers
7+
"""
8+
9+
10+
def fibonacci(n):
11+
""" compute the nth Fibonacci number """
12+
13+
if n < 0:
14+
return None
15+
elif n == 0:
16+
return 0
17+
if n == 1:
18+
return 1
19+
else:
20+
return fibonacci(n - 1) + fibonacci(n - 2)
21+
22+
23+
def lucas(n):
24+
""" compute the nth Lucas number """
25+
26+
if n < 0:
27+
return None
28+
if n == 0:
29+
return 2
30+
elif n == 1:
31+
return 1
32+
else:
33+
return lucas(n - 1) + lucas(n - 2)
34+
35+
36+
def sum_series(n, n0=0, n1=1):
37+
"""
38+
compute the nth value of a summation series.
39+
40+
:param n0=0: value of zeroth element in the series
41+
:param n1=1: value of first element in the series
42+
43+
if n0 == 0 and n1 == 1, the result is the Fibbonacci series
44+
45+
if n0 == 2 and n1 == 1, the result is the Lucas series
46+
"""
47+
if n < 0:
48+
return None
49+
if n == 0:
50+
return n0
51+
elif n == 1:
52+
return n1
53+
else:
54+
return sum_series(n - 1, n0, n1) + sum_series(n - 2, n0, n1)
55+
56+
57+
if __name__ == "__main__":
58+
# run some tests
59+
60+
assert fibonacci(-1) is None
61+
assert fibonacci(-23) is None
62+
63+
assert fibonacci(0) == 0
64+
assert fibonacci(1) == 1
65+
assert fibonacci(2) == 1
66+
assert fibonacci(3) == 2
67+
assert fibonacci(4) == 3
68+
assert fibonacci(5) == 5
69+
assert fibonacci(6) == 8
70+
assert fibonacci(7) == 13
71+
72+
assert lucas(-1) is None
73+
assert lucas(-23) is None
74+
75+
# do these with a loop:
76+
tests = [(0, 2),
77+
(1, 1),
78+
(2, 3),
79+
(3, 4),
80+
(4, 7),
81+
(5,11),
82+
(6,18),
83+
(7, 29),
84+
]
85+
for input, output in tests:
86+
assert lucas(input) == output
87+
88+
# test if sum_series matched Fibonacci
89+
for n in range(0, 10):
90+
assert sum_series(n) == fibonacci(n)
91+
92+
# test if sum_series matched lucas
93+
for n in range(0, 10):
94+
assert sum_series(n, 2, 1) == lucas(n)
95+
96+
print "tests passed"

0 commit comments

Comments
 (0)