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
40 changes: 40 additions & 0 deletions Students/Danielle_Marcos/session02/ack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def ack (m, n):
"""Return a value based on the Ackermann-Peter function."""

if 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))
elif m < 0 and n < 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

what will happen if n < 0 and m == 0?

and what about m < 0, n > 0?

But otherwise, looks good.

return None

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,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

print""
print "All Tests Pass"
Copy link
Contributor

Choose a reason for hiding this comment

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

good idea to test the n, m < 0 cases, too.

74 changes: 74 additions & 0 deletions Students/Danielle_Marcos/session02/series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
def fibonacci(n):
"""Return the n value based on the Fibonacci series."""
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)

def lucas(n):
"""Return the n value in the Lucas Numbers."""
if n <= 0:
return 2
elif n == 1:
return 1
else:
return lucas(n-1) + lucas(n-2)

def sum_series(n, x=0, y=1):
if n <= 0:
return x
Copy link
Contributor

Choose a reason for hiding this comment

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

what do you want for n < 0? Is it defined?

elif n == 1:
return y
else:
return sum_series(n-1, x, y) + sum_series(n-2, x, y)

if __name__ == "__main__":
"""Validate that when an nth values are passed different positions are returned"""

# Validate Fibonacci tests pass successfully.
assert fibonacci(0) == 0
assert fibonacci(1) == 1
assert fibonacci(2) == 1
assert fibonacci(3) == 2
assert fibonacci(4) == 3
assert fibonacci(5) == 5
assert fibonacci(6) == 8
assert fibonacci(7) == 13

# Validate Lucas tests pass successfully.

assert lucas(0) == 2
assert lucas(1) == 1
assert lucas(2) == 3
assert lucas(3) == 4
assert lucas(4) == 7
assert lucas(5) == 11
assert lucas(6) == 18
assert lucas(7) == 29

# Validate sum_series tests for Fibonacci pass successfully.

assert sum_series(0) == 0
assert sum_series(1) == 1
assert sum_series(2) == 1
assert sum_series(3) == 2
assert sum_series(4) == 3
assert sum_series(5) == 5
assert sum_series(6) == 8
assert sum_series(7) == 13

# Validate sum_series tests for Lucas pass successfully.
Copy link
Contributor

Choose a reason for hiding this comment

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

oops, looks like you forgot to update test for Lucas!


assert sum_series(0) == 0
assert sum_series(1) == 1
assert sum_series(2) == 1
assert sum_series(3) == 2
assert sum_series(4) == 3
assert sum_series(5) == 5
assert sum_series(6) == 8
assert sum_series(7) == 13

print ""
print "All Tests Pass"