Skip to content
File renamed without changes.
46 changes: 46 additions & 0 deletions Students/RPerkins/session02/ack.py
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

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.

nice! where did you find that?

Copy link
Copy Markdown
Author

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:

In Students/RPerkins/session02/ack.py:

  • 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) == 222-3

nice! where did you find that?


Reply to this email directly or view it on GitHub
https://github.com/UWPCE-PythonCert/IntroToPython/pull/12/files#r18753604
.

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

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.

On Sun, Oct 12, 2014 at 9:36 PM, Robert Perkins notifications@github.com
wrote:

I thought the HW instructions said to go up to m=4, n=4 on the table in
the Wikipedia article,

yup -- I wanted you to hit that recursion error... :-)

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).

ahh -- I had forgotten those where there.

Nice job.

-Chris

Christopher Barker, PhD

Python Language Consulting

  • Teaching
  • Scientific Software Development
  • Desktop GUI and Web Development
  • wxPython, numpy, scipy, Cython

#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'
93 changes: 93 additions & 0 deletions Students/RPerkins/session02/series.py
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

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 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)"""

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.

:-)


# 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