-
Notifications
You must be signed in to change notification settings - Fork 22
/
recursion.py
89 lines (70 loc) · 2.94 KB
/
recursion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!python
import unittest
def factorial(n):
"""factorial(n) returns the product of the integers 1 through n for n >= 0,
otherwise raises ValueError for n < 0 or non-integer n"""
# implement factorial_iterative and factorial_recursive below, then
# change this to call your implementation to verify it passes all tests
# return factorial_iterative(n)
return factorial_recursive(n)
def factorial_iterative(n):
# TODO: implement the factorial function iteratively here
pass
# once implemented, change factorial (above) to call factorial_iterative
# to verify that your iterative implementation passes all tests below
def factorial_recursive(n):
# check if n is negative or not an integer (invalid input)
if n < 0 or not isinstance(n, int):
raise ValueError('factorial is undefined for n = {}'.format(n))
# check if n is one of the base cases
elif n == 0 or n == 1:
return 1
# check if n is an integer larger than the base cases
elif n > 1:
# call function recursively
return n * factorial_recursive(n - 1)
def fibonacci(n):
"""fibonacci(n) returns the n-th number in the Fibonacci sequence,
which is defined with the recurrence relation:
fibonacci(0) = 0
fibonacci(1) = 1
fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2), for n > 1"""
# check if n is negative or not an integer (invalid input)
if n < 0 or not isinstance(n, int):
raise ValueError('fibonacci is undefined for n = {}'.format(n))
# implement fibonacci_recursive, _memoized, and _dynamic below, then
# change this to call your implementation to verify it passes all tests
return fibonacci_recursive(n)
# return fibonacci_memoized(n)
# return fibonacci_dynamic(n)
def fibonacci_recursive(n):
# check if n is one of the base cases
if n == 0 or n == 1:
return n
# check if n is an integer larger than the base cases
elif n > 1:
# call function recursively
return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)
def fibonacci_memoized(n):
# TODO: memoize the fibonacci function's recursive implementation here
pass
# once implemented, change fibonacci (above) to call fibonacci_memoized
# to verify that your memoized implementation passes all test cases
def fibonacci_dynamic(n):
# TODO: implement the fibonacci function with dynamic programming here
pass
# once implemented, change fibonacci (above) to call fibonacci_dynamic
# to verify that your dynamic implementation passes all test cases
def main():
import sys
args = sys.argv[1:] # Ignore script file name
if len(args) == 1:
num = int(args[0])
# result = factorial(num)
# print('factorial({}) => {}'.format(num, result))
result = fibonacci(num)
print('fibonacci({}) => {}'.format(num, result))
else:
print('Usage: {} number'.format(sys.argv[0]))
if __name__ == '__main__':
main()