Skip to content

Commit cc04121

Browse files
author
Alex Galvin
committed
turning in sessions02 homework for alex galvin
1 parent b00e301 commit cc04121

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎Students/alexg/session02/ack.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/python
2+
3+
def ack(m,n):
4+
"""Returns the output of the Ackerman Function"""
5+
if m<0 or n<0:
6+
return None
7+
elif not m:
8+
return n+1
9+
elif m>0 and not n:
10+
return ack(m-1,1)
11+
elif m>0 and n>0:
12+
return ack(m-1, ack(m,n-1))
13+
14+
if __name__ == "__main__":
15+
assert ack(0,0)==1
16+
assert ack(0,1)==2
17+
assert ack(0,2)==3
18+
assert ack(0,3)==4
19+
assert ack(0,4)==5
20+
assert ack(1,0)==2
21+
assert ack(1,2)==4
22+
assert ack(1,3)==5
23+
assert ack(1,4)==6
24+
assert ack(2,0)==3
25+
assert ack(2,1)==5
26+
assert ack(2,2)==7
27+
assert ack(2,3)==9
28+
assert ack(2,4)==11
29+
assert ack(3,0)==5
30+
assert ack(3,1)==13
31+
assert ack(3,2)==29
32+
assert ack(3,3)==61
33+
assert ack(3,4)==125
34+
print "All Tests Pass"

‎Students/alexg/session02/series.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/python
2+
3+
def fibonacci(x):
4+
"""Returns the nth value of the Fibonacci Series"""
5+
if not x:
6+
return 0
7+
elif x == 1:
8+
return 1
9+
else:
10+
return fibonacci(x-1)+fibonacci(x-2)
11+
12+
def lucas(x):
13+
"""Returns the nth value of the Lucas Series"""
14+
if not x:
15+
return 2
16+
elif x == 1:
17+
return 1
18+
else:
19+
return lucas(x-1)+lucas(x-2)
20+
21+
def sum_series(n,x=0,y=1):
22+
"""Returns the nth value of either series, or creates a new one"""
23+
if x==0 and y==1:
24+
return fibonacci(n)
25+
elif x==2 and y==1:
26+
return lucas(n)
27+
else:
28+
if not n:
29+
return x
30+
elif n==1:
31+
return y
32+
else:
33+
return sum_series(n-1)+sum_series(n-2)
34+
35+
if __name__ == "__main__":
36+
#Testing the 7th value of the fibonacci sequence
37+
assert fibonacci(6)==8
38+
39+
#Testing the 7th value of the lucas sequence
40+
assert lucas(6)==18
41+
42+
#Testing for the 2nd value of the new function
43+
assert sum_series(9,40,2)==34

0 commit comments

Comments
 (0)