Skip to content

Commit da956ef

Browse files
committed
added series hw
1 parent bc0ea78 commit da956ef

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
def fib(n):
2+
"""Return the nth value in the fibonacci function, which is a recursive function. """
3+
if n == 0:
4+
return 0
5+
elif n == 1:
6+
return 1
7+
else:
8+
return fib(n-1) + fib(n-2)
9+
10+
# print fib(7)
11+
12+
13+
def lucas(n):
14+
"""Return the nth value in the Lucas function, which is also a recursive function but starts with 2 and 1 rather than 0 and 1"""
15+
if n == 0:
16+
return 2
17+
elif n == 1:
18+
return 1
19+
else:
20+
return lucas(n-1) + lucas(n-2)
21+
# print lucas (2)
22+
# print lucas (7)
23+
24+
25+
def sum_series(n, x=0,y=1):
26+
""" Return the nth value of a recursive function. There are two optional parameters, x and y.
27+
x is the 0th value and y is the 1st value.
28+
29+
When x = 0 and y = 1, the result is the fibbonacci sequence
30+
When x = 2 and y = 1, the result is the lucas sequence
31+
When x and y are other values, there are different sequences
32+
"""
33+
if n == 0:
34+
return x
35+
elif n == 1:
36+
return y
37+
else:
38+
return sum_series(n-1, x, y) + sum_series(n-2, x, y) # it took me a while to figure out why it was going back to fibbonacci
39+
# even when x =2, of course have to re-add x and y in the else part!!!
40+
41+
# print sum_series(0,2)
42+
# print sum_series(1,0,4)
43+
# print sum_series(2)
44+
# print sum_series(7)
45+
# print sum_series(7,2)
46+
# print sum_series(7,3,1)
47+
48+
49+
if __name__ == "__main__":
50+
assert fib(7) == 13
51+
assert lucas (7) == 29
52+
assert sum_series(7) == 13
53+
assert sum_series(7,2) == 29
54+
assert sum_series(7,3) == 37
55+
56+
print "all good here"

0 commit comments

Comments
 (0)