Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Students/alexg/session02/ack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/python

def ack(m,n):
"""Returns the output of the Ackerman Function"""
if m<0 or n<0:
return None
elif not m:
return n+1
elif m>0 and not n:
return ack(m-1,1)
elif m>0 and n>0:
return ack(m-1, ack(m,n-1))

if __name__ == "__main__":
assert ack(0,0)==1
assert ack(0,1)==2
assert ack(0,2)==3
assert ack(0,3)==4
assert ack(0,4)==5
assert ack(1,0)==2
assert ack(1,2)==4
assert ack(1,3)==5
assert ack(1,4)==6
assert ack(2,0)==3
assert ack(2,1)==5
assert ack(2,2)==7
assert ack(2,3)==9
assert ack(2,4)==11
assert ack(3,0)==5
assert ack(3,1)==13
assert ack(3,2)==29
assert ack(3,3)==61
assert ack(3,4)==125
print "All Tests Pass"
43 changes: 43 additions & 0 deletions Students/alexg/session02/series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/python

def fibonacci(x):
"""Returns the nth value of the Fibonacci Series"""
if not x:
return 0
elif x == 1:
return 1
else:
return fibonacci(x-1)+fibonacci(x-2)

def lucas(x):
"""Returns the nth value of the Lucas Series"""
if not x:
return 2
elif x == 1:
return 1
else:
return lucas(x-1)+lucas(x-2)

def sum_series(n,x=0,y=1):
"""Returns the nth value of either series, or creates a new one"""
if x==0 and y==1:
return fibonacci(n)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense, but as the rest of the code can compute the Fibonacci and Lucas series, you may as well jsut use that. i.e. the general form usually doesn't call a more specific version, rather the other way around. Of course, that all got confused by by the fact that we asked you to write the other two first...

elif x==2 and y==1:
return lucas(n)
else:
if not n:
return x
elif n==1:
return y
else:
return sum_series(n-1)+sum_series(n-2)

if __name__ == "__main__":
#Testing the 7th value of the fibonacci sequence
assert fibonacci(6)==8

#Testing the 7th value of the lucas sequence
assert lucas(6)==18

#Testing for the 2nd value of the new function
assert sum_series(9,40,2)==34