Repository navigation
Expand file tree
/
Copy pathseries_non_recusive.py
More file actions
83 lines (65 loc) · 2.18 KB
/
Copy pathseries_non_recusive.py
File metadata and controls
83 lines (65 loc) · 2.18 KB
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
#!/usr/bin/env python3
"""
a template for the series assignment
"""
def fibonacci(n):
""" compute the nth Fibonacci number """
a, b = 0, 1
if n == 0:
return a
for _ in range(n - 1):
a, b = b, a + b
return b
def lucas(n):
""" compute the nth Lucas number """
a, b = 2, 1 # notice that all I had to change from fib were these values?
if n == 0:
return a
for _ in range(n - 1):
a, b = b, a + b
return b
def sum_series(n, n0=0, n1=1):
"""
compute the nth value of a summation series.
:param n0=0: value of zeroth element in the series
:param n1=1: value of first element in the series
This function should generalize the fibonacci() and the lucas(),
so that this function works for any first two numbers for a sum series.
Once generalized that way, sum_series(n, 0, 1) should be equivalent to fibonacci(n).
And sum_series(n, 2, 1) should be equivalent to lucas(n).
sum_series(n, 3, 2) should generate antoehr series with no specific name
The defaults are set to 0, 1, so if you don't pass in any values, you'll
get the fibonacci sercies
"""
a, b = n0, n1 # notice that all I had to change from fib were these values?
if n == 0:
return a
for _ in range(n - 1):
a, b = b, a + b
return b
if __name__ == "__main__":
# run some tests
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
assert lucas(0) == 2
assert lucas(1) == 1
assert lucas(4) == 7
# test that sum_series matches fibonacci
assert sum_series(5) == fibonacci(5)
assert sum_series(7, 0, 1) == fibonacci(7)
# test if sum_series matched lucas
assert sum_series(5, 2, 1) == lucas(5)
# test if sum_series works for arbitrary initial values
assert sum_series(0, 3, 2) == 3
assert sum_series(1, 3, 2) == 2
assert sum_series(2, 3, 2) == 5
assert sum_series(3, 3, 2) == 7
assert sum_series(4, 3, 2) == 12
assert sum_series(5, 3, 2) == 19
print("tests passed")