-
Notifications
You must be signed in to change notification settings - Fork 76
Adding session02 homework #12
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
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
734496e
Removing old HW
97a581f
Remove old HW
7f18a5e
Added session01 directory and HW files
c364569
Added HW file 1
f66d2aa
Added first working ack.py
ff4d314
Fixed syntax for None comparisons in test block
92059fb
Added success message to testing block
51d47ec
Added series.py module with empty fibonacci function
ed25cb1
Merge branch 'session02'
512365b
Added fibonacci and lucas series code and test blocks
9986a2b
added sum_series function and partial test block
e8dc4a7
Fixed missing params in sum_series recursive calls and added final te…
6894a68
added test block comments
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
File renamed without changes.
File renamed without changes.
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,46 @@ | ||
| __author__ = 'Robert W. Perkins' | ||
|
|
||
|
|
||
| def ack(m, n): | ||
| """Return the result of the Ackermann function on m and n""" | ||
| if m < 0 or n < 0: | ||
| return None | ||
| elif m == 0: | ||
| return n+1 | ||
| elif m > 0 and n == 0: | ||
| return ack(m-1, 1) | ||
| elif m > 0 and n > 0: | ||
| return ack(m-1, ack(m, n-1)) | ||
|
|
||
| # Testing Block | ||
| if __name__ == "__main__": | ||
| assert ack(-1, 2) is None | ||
| assert ack(2, -1) is None | ||
| assert ack(-1, -1) is None | ||
| 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, 1) == 3 | ||
| 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 | ||
| # max recursion depth exceeded after this point | ||
| #assert ack(4, 0) == 2**2**2-3 | ||
| #assert ack(4, 1) == 2**2**2**2-3 | ||
| #assert ack(4, 2) == (2**65536)-3 | ||
| #assert ack(4, 3) == (2**2**65536)-3 | ||
| #assert ack(4, 4) == (2**2**2**65536)-3 | ||
| print 'All tests passed' | ||
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,93 @@ | ||
| __author__ = 'Robert W. Perkins' | ||
|
|
||
|
|
||
| def fibonacci(n): | ||
| """Return the 'n'th value in the Fibonacci series""" | ||
|
|
||
| # test for input in valid range | ||
| if n < 0: | ||
| return None | ||
|
|
||
| # test for n == 0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is exactly the kind of not-very-useful comment you shouldn't bother with ;-) |
||
| if n == 0: | ||
| return 0 | ||
|
|
||
| # base case is n == 1 | ||
| if n == 1: | ||
| return 1 | ||
| else: | ||
| return fibonacci(n-1) + fibonacci(n-2) | ||
|
|
||
| def lucas(n): | ||
| """Return the 'n'th value in the Lucas number series""" | ||
|
|
||
| # test for input in valid range | ||
| if n < 0: | ||
| return None | ||
|
|
||
| # test for n == 0 | ||
| if n == 0: | ||
| return 2 | ||
|
|
||
| # base case is n == 1 | ||
| if n == 1: | ||
| return 1 | ||
| else: | ||
| return lucas(n-1) + lucas(n-2) | ||
|
|
||
| def sum_series(n,first=0,second=1): | ||
| """Return the 'n'th value of a variable series""" | ||
|
|
||
| """The first two numbers are passed via "first" and "second" | ||
| and each subsequent element is the sum of the 'n-1'th and 'n-2'th element (yes, i said tooth)""" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :-) |
||
|
|
||
| # test for input in valid range | ||
| if n < 0: | ||
| return None | ||
|
|
||
| # test for n == 0 | ||
| if n == 0: | ||
| return first | ||
|
|
||
| # base case is n == 1 | ||
| if n == 1: | ||
| return second | ||
| else: | ||
| return sum_series(n-1,first,second) + sum_series(n-2,first,second) | ||
|
|
||
|
|
||
| # Testing Block | ||
| if __name__ == "__main__": | ||
|
|
||
| # test fibonacci function for out of range, n == zero, base case, and 18th value in series | ||
| assert fibonacci(-3) is None | ||
| assert fibonacci(0) == 0 | ||
| assert fibonacci(1) == 1 | ||
| assert fibonacci(17) == 1597 | ||
|
|
||
| # test lucas function for out of range, n == zero, base case, and 11th value in series | ||
| assert lucas(-3) is None | ||
| assert lucas(0) == 2 | ||
| assert lucas(1) == 1 | ||
| assert lucas(10) == 123 | ||
|
|
||
| # tests sum_series function for fibonacci behavior with no optional params | ||
| # for out of range, n == zero, base case, and 18th value in series | ||
| assert sum_series(-3) is None | ||
| assert sum_series(0) == 0 | ||
| assert sum_series(1) == 1 | ||
| assert sum_series(17) == 1597 | ||
|
|
||
| # tests sum_series function for lucas behavior with lucas series params | ||
| # for out of range, n == zero, base case, and 11th value in series | ||
| assert sum_series(-3,2,1) is None | ||
| assert sum_series(0,2,1) == 2 | ||
| assert sum_series(1,2,1) == 1 | ||
| assert sum_series(10,2,1) == 123 | ||
|
|
||
| # tests sum_series function for valid results with optional params | ||
| # for out of range, n == zero, base case, and 11th value in series | ||
| assert sum_series(-3,4,2) is None | ||
| assert sum_series(0,4,2) == 4 | ||
| assert sum_series(1,4,2) == 2 | ||
| assert sum_series(10,4,2) == 246 | ||
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.
nice! where did you find that?
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.
I thought the HW instructions said to go up to m=4, n=4 on the table in the
Wikipedia article, so I started out with an assert statement for each cell
in the table (the table gave the higher order answers as powers of two).
The max recursion depth error message was thrown by the interpreter on the
first run, so I just commented them out one by one till it stopped
complaining.
On Sun, Oct 12, 2014 at 9:27 PM, Christopher H.Barker, PhD <
notifications@github.com> wrote:
Robert Perkins
Mobile: 425-239-9269
Email: meshmote@gmail.com
Professional Profile
http://www.linkedin.com/pub/robert-perkins/9/a6a/a91
http://maps.google.com/maps?q=&hl=en
See who we know in common
http://www.linkedin.com/e/wwk/29505421/?hs=false&tok=0zCreXIp0dhRQ1Want a
signature like this?
http://www.linkedin.com/e/sig/29505421/?hs=false&tok=1-_hPrm6YdhRQ1
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.
On Sun, Oct 12, 2014 at 9:36 PM, Robert Perkins notifications@github.com
wrote:
Nice job.
-Chris
Christopher Barker, PhD
Python Language Consulting