-
Notifications
You must be signed in to change notification settings - Fork 77
Ian M. Davis Homework #15
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
6 commits
Select commit
Hold shift + click to select a range
f6bc32e
Added the Python function to evaluate the Ackermann function to the s…
imdavis 162ed4d
First commit of series.py function. Calculates Fibanocci series up t…
imdavis de1962b
Commit of series.py after adding a new function to evaluate the nth t…
imdavis 6cf06d7
Added the sum_series function to the series.py module. Has default z…
imdavis 003ca7f
For completeness, push up the homework assignment from the first sess…
imdavis ab822ab
Push up my lightening talk too.
imdavis 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
Binary file not shown.
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,26 @@ | ||
| # a function which takes on a single argument defining the size | ||
| # of a grid to draw | ||
|
|
||
| def print_grid(gridsize): | ||
| # define the top and bottom portion of a single grid cell | ||
| corner = "+" | ||
| midtopbot = 4 * "-" | ||
|
|
||
| # define the center of a single grid cell | ||
| gridcenter = "|" + 4 * " " | ||
|
|
||
| # define how to draw the complete top, bottom, and middle of the | ||
| # full grid in a single direction | ||
| topbot = gridsize * (corner + midtopbot) + corner | ||
| center = gridsize * gridcenter + "|" | ||
|
|
||
| # build the grid in one direction | ||
| onedirgrid = topbot + "\n" | ||
| onedirgrid += 4 * (center + "\n") | ||
|
|
||
| #build the full grid | ||
| grid = gridsize * onedirgrid + topbot | ||
|
|
||
| # now print the grid | ||
| print grid | ||
|
|
||
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,10 @@ | ||
| # draws a grid using the buildgrid function after prompting the | ||
| # user for the size of the grid | ||
|
|
||
| import buildgrid | ||
|
|
||
| try: | ||
| size = int(raw_input("What size grid would you like to build? ")) | ||
| buildgrid.print_grid(size) | ||
| except ValueError: | ||
|
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. jumping ahead to Exceptions, I see -- nicely done. |
||
| print "Please enter an integer!" | ||
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,78 @@ | ||
| #!/usr/bin/env python2.7 | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| """Example of a recursive function with a good docstring. | ||
|
|
||
| For best results to view the docstring, after importing do: | ||
|
|
||
| >>> print ack.__doc__ | ||
|
|
||
| Its value grows rapidly, even for small inputs. For example A(4,2) is an | ||
| integer of 19,729 decimal digits. May hit maximum recursion limit. See: | ||
|
|
||
| sys.getrecursionlimit() | ||
| sys.setrecursionlimit() | ||
|
|
||
| """ | ||
|
|
||
| def ack(m, n): | ||
| """Evaluation of the Ackermann Function. | ||
|
|
||
| The Ackermann Function is defined as: | ||
| A(m, n) = | ||
| n+1 if m = 0 | ||
| A(m−1, 1) if m > 0 and n = 0 | ||
| A(m−1, A(m, n−1)) if m > 0 and n > 0 | ||
|
|
||
| Args: | ||
| m (int): must be >= 0 | ||
| n (int): must be >= 0. | ||
|
|
||
| Yields: | ||
| Evaluation of Ackermann function for A(m, n) | ||
|
|
||
| """ | ||
|
|
||
| if (m < 0 or n < 0): | ||
| print "Arguments for the Ackermann function must be >= 0." | ||
| return None | ||
| elif (m == 0): | ||
| return n + 1 | ||
| elif (n == 0): | ||
| return ack(m - 1, 1) | ||
| else: | ||
| return ack(m - 1, ack(m, n - 1)) | ||
|
|
||
| if __name__ == '__main__': | ||
| TestsPass = True | ||
|
|
||
| try: | ||
| 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 | ||
| assert ack(4, 0) == 13 | ||
|
|
||
| except AssertionError: | ||
| TestsPass = False | ||
| print "All Tests Did Not Pass!" | ||
|
|
||
| if (TestsPass): | ||
| 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,127 @@ | ||
| #!/usr/bin/env python2.7 | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| def fibonacci(n): | ||
| """Evaluation of the nth term of the Fibonnaci Series. | ||
|
|
||
| The Fibonnaci Series is defined as: | ||
| 0th term: 0 | ||
| 1st term: 1 | ||
| nth term: sum of previous two terms in the series | ||
|
|
||
| Args: | ||
| n (int): nth term in series (must be >= 0) | ||
|
|
||
| Yields: | ||
| nth term of the Fibonnaci Series. | ||
|
|
||
| """ | ||
|
|
||
| if (n < 0): | ||
| print "Arguments for the Fibonnaci series must be >= 0." | ||
| return None | ||
| elif (n == 0): # zeroth term = 0 | ||
| return 0 | ||
| elif (n == 1): # first term = 1 | ||
| return 1 | ||
| else: | ||
| return fibonacci(n - 1) + fibonacci(n - 2) | ||
|
|
||
| def lucas(n): | ||
| """Evaluation of the nth term of the Lucas Series. | ||
|
|
||
| The Lucas Series is defined as: | ||
| 0th term: 2 | ||
| 1st term: 1 | ||
| nth term: sum of previous two terms in the series | ||
|
|
||
| Args: | ||
| n (int): nth term in series (must be >= 0) | ||
|
|
||
| Yields: | ||
| nth term of the Lucas Series. | ||
|
|
||
| """ | ||
|
|
||
| if (n < 0): | ||
| print "Arguments for the Lucas series must be >= 0." | ||
| return None | ||
| elif (n == 0): # zeroth term = 0 | ||
| return 2 | ||
| elif (n == 1): # first term = 1 | ||
| return 1 | ||
| else: | ||
| return lucas(n - 1) + lucas(n - 2) | ||
|
|
||
| def sum_series(n, zerothTerm=0, firstTerm=1): | ||
| """Evaluation of the nth term of a series. | ||
|
|
||
| This function will return the nth term in a series which is the sum | ||
| of the previous two consecutive terms in the series. The user can | ||
| specify any zeroth and first terms to define the series, or can use | ||
| the default values of 0 and 1 to give the n-th term of the | ||
| Fibonacci series. | ||
|
|
||
| Args: | ||
| n (int): nth term in series (must be >= 0) | ||
| zerothTerm (int): zeroth term in the series (default = 0) | ||
| firstTerm (int): first term in the series (default = 1) | ||
|
|
||
| Yields: | ||
| nth term of the series. | ||
|
|
||
| """ | ||
|
|
||
| if (n < 0): | ||
| print "Arguments for the series must be >= 0." | ||
| return None | ||
| elif (n == 0): | ||
| return zerothTerm | ||
| elif (n == 1): | ||
| return firstTerm | ||
| else: | ||
| return sum_series(n - 1, zerothTerm, firstTerm) + sum_series(n - 2, zerothTerm, firstTerm) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| FibanocciTestsPass = True | ||
| LucasTestsPass = True | ||
| ArbitrarySeriesTestsPass = True | ||
|
|
||
| # Some assertions to test the Fibonacci series function against known solutions | ||
| try: | ||
| assert fibonacci(0) == 0 | ||
| assert fibonacci(1) == 1 | ||
| assert fibonacci(10) == 55 | ||
| assert fibonacci(19) == 4181 | ||
|
|
||
| except AssertionError: | ||
| FibanocciTestsPass = False | ||
| print "All Fibonacci Tests Did Not Pass!" | ||
|
|
||
| # Some assertions to test the Lucas series function against known solutions | ||
| try: | ||
| assert lucas(0) == 2 | ||
| assert lucas(1) == 1 | ||
| assert lucas(10) == 123 | ||
| assert lucas(20) == 15127 | ||
|
|
||
| except AssertionError: | ||
| LucasTestsPass = False | ||
| print "All Lucas Tests Did Not Pass!" | ||
|
|
||
| # Some assertions to test the arbitrary series function against known solutions | ||
| try: | ||
| assert sum_series(10) == 55 | ||
| assert sum_series(10, 0, 1) == 55 | ||
| assert sum_series(10, 2, 1) == 123 | ||
| assert sum_series(15, 3, 3) == 2961 | ||
| assert sum_series(20, 5, 2) == 34435 | ||
| assert sum_series(20, 2, 5) == 42187 | ||
|
|
||
| except AssertionError: | ||
| ArbitrarySeriesTestsPass = False | ||
| print "All aritrary series tests did not pass" | ||
|
|
||
| if (FibanocciTestsPass and LucasTestsPass and ArbitrarySeriesTestsPass): | ||
| print "All Tests Pass!" |
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 use of defined constants and string multiplication