Repository navigation
turning in sessions02 homework for alex galvin #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+77
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| 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 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...