Skip to content

Commit e1f1e76

Browse files
committed
updated series template
1 parent 03a159b commit e1f1e76

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

source/exercises/series_template.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ def sum_series(n, n0=0, n1=1):
2121
2222
:param n0=0: value of zeroth element in the series
2323
:param n1=1: value of first element in the series
24-
24+
2525
This function should generalize the fibonacci() and the lucas(),
2626
so that this function works for any first two numbers for a sum series.
2727
Once generalized that way, sum_series(n, 0, 1) should be equivalent to fibonacci(n).
2828
And sum_series(n, 2, 1) should be equivalent to lucas(n).
29+
30+
sum_series(n, 3, 2) should generate antoehr series with no specific name
31+
32+
The defaults are set to 0, 1, so if you don't pass in any values, you'll
33+
get the fibonacci sercies
2934
"""
3035
pass
3136

@@ -45,9 +50,19 @@ def sum_series(n, n0=0, n1=1):
4550

4651
assert lucas(4) == 7
4752

53+
# test that sum_series matches fibonacci
4854
assert sum_series(5) == fibonacci(5)
55+
assert sum_series(7, 0, 1) == fibonacci(7)
4956

5057
# test if sum_series matched lucas
5158
assert sum_series(5, 2, 1) == lucas(5)
5259

60+
# test if sum_series works for arbitrary initial values
61+
assert sum_series(0, 3, 2) == 3
62+
assert sum_series(1, 3, 2) == 2
63+
assert sum_series(2, 3, 2) == 5
64+
assert sum_series(3, 3, 2) == 7
65+
assert sum_series(4, 3, 2) == 12
66+
assert sum_series(5, 3, 2) == 19
67+
5368
print("tests passed")
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
a template for the series assignment
5+
"""
6+
7+
8+
def fibonacci(n):
9+
""" compute the nth Fibonacci number """
10+
a, b = 0, 1
11+
if n == 0:
12+
return a
13+
for _ in range(n - 1):
14+
a, b = b, a + b
15+
return b
16+
17+
18+
def lucas(n):
19+
""" compute the nth Lucas number """
20+
pass
21+
22+
23+
def sum_series(n, n0=0, n1=1):
24+
"""
25+
compute the nth value of a summation series.
26+
27+
:param n0=0: value of zeroth element in the series
28+
:param n1=1: value of first element in the series
29+
30+
This function should generalize the fibonacci() and the lucas(),
31+
so that this function works for any first two numbers for a sum series.
32+
Once generalized that way, sum_series(n, 0, 1) should be equivalent to fibonacci(n).
33+
And sum_series(n, 2, 1) should be equivalent to lucas(n).
34+
35+
sum_series(n, 3, 2) should generate antoehr series with no specific name
36+
37+
The defaults are set to 0, 1, so if you don't pass in any values, you'll
38+
get the fibonacci sercies
39+
"""
40+
pass
41+
42+
if __name__ == "__main__":
43+
# run some tests
44+
assert fibonacci(0) == 0
45+
assert fibonacci(1) == 1
46+
assert fibonacci(2) == 1
47+
assert fibonacci(3) == 2
48+
assert fibonacci(4) == 3
49+
assert fibonacci(5) == 5
50+
assert fibonacci(6) == 8
51+
assert fibonacci(7) == 13
52+
53+
assert lucas(0) == 2
54+
assert lucas(1) == 1
55+
56+
assert lucas(4) == 7
57+
58+
# test that sum_series matches fibonacci
59+
assert sum_series(5) == fibonacci(5)
60+
assert sum_series(7, 0, 1) == fibonacci(7)
61+
62+
# test if sum_series matched lucas
63+
assert sum_series(5, 2, 1) == lucas(5)
64+
65+
# test if sum_series works for arbitrary initial values
66+
assert sum_series(0, 3, 2) == 3
67+
assert sum_series(1, 3, 2) == 2
68+
assert sum_series(2, 3, 2) == 5
69+
assert sum_series(3, 3, 2) == 7
70+
assert sum_series(4, 3, 2) == 12
71+
assert sum_series(5, 3, 2) == 19
72+
73+
print("tests passed")

0 commit comments

Comments
 (0)