1+ __author__ = 'carolyn.evans'
2+
3+ def fibonacci (n ):
4+ """ This is the Fibonacci Function.
5+ It is a series of numbers that is seeded with 0 and 1, and then each subsequent
6+ number in the series is the sum of the prior two numbers.
7+
8+ :param n: Parameter n designates which value in the fibonacci series to return.
9+ :return: An integer from the fibonacci series based on the given n is returned.
10+ If a negative number is supplied as n, the function returns None.
11+ """
12+
13+ import sys
14+
15+ try :
16+ if n < 0 :
17+ return None
18+
19+ if n <= 1 :
20+ return n
21+
22+ return fibonacci (n - 1 )+ fibonacci (n - 2 )
23+
24+ except Exception , err :
25+ sys .stderr .write ('ERROR: %s\n ' % str (err ))
26+ return None
27+
28+
29+ def lucas (n ):
30+ """ This is the Lucas Function.
31+ It is a series of numbers that is seeded with 2 and 1, and then each subsequent
32+ number in the series is the sum of the prior two numbers.
33+
34+ :param n: Parameter n designates which value in the lucas series to return.
35+ :return: An integer from the lucas series based on the given n is returned.
36+ If a number less than 1 is supplied as n, the function returns None.
37+ """
38+
39+ import sys
40+
41+ try :
42+ if n < 0 :
43+ return None
44+
45+ if n == 0 :
46+ return 2
47+
48+ if n == 1 :
49+ return 1
50+
51+ return lucas (n - 1 )+ lucas (n - 2 )
52+
53+ except Exception , err :
54+ sys .stderr .write ('ERROR: %s\n ' % str (err ))
55+ return None
56+
57+
58+ def sum_series (n , firstValue = 0 , secondValue = 1 ):
59+ """ This function produces a number from a series of numbers that is seeded with
60+ firstValue and secondValue parameters, and then each subsequent number is the
61+ sum of the previous two numbers.
62+
63+ :param n: Parameter n designates which value in the series to return.
64+ :param firstValue: This is the first value in the series.
65+ :param secondValue: This is the second value in the series.
66+ :return: An integer from the a series based on the given parameters is returned.
67+ """
68+
69+ import sys
70+
71+ try :
72+ if n < 0 :
73+ return None
74+
75+ if n == 0 :
76+ return firstValue
77+
78+ if n == 1 :
79+ return secondValue
80+
81+ return sum_series (n - 1 , firstValue , secondValue )+ sum_series (n - 2 , firstValue , secondValue )
82+
83+ except Exception , err :
84+ sys .stderr .write ('ERROR: %s\n ' % str (err ))
85+ return None
86+
87+
88+ if __name__ == "__main__" :
89+ # test cases for fibonacci()
90+ assert fibonacci (- 1 ) == None
91+ assert fibonacci (0 ) == 0
92+ assert fibonacci (1 ) == 1
93+ assert fibonacci (2 ) == 1
94+ assert fibonacci (3 ) == 2
95+ assert fibonacci (4 ) == 3
96+ assert fibonacci (5 ) == 5
97+ assert fibonacci (6 ) == 8
98+ assert fibonacci (7 ) == 13
99+
100+ # test cases for lucas()
101+ assert lucas (- 1 ) == None
102+ assert lucas (0 ) == 2
103+ assert lucas (1 ) == 1
104+ assert lucas (2 ) == 3
105+ assert lucas (3 ) == 4
106+ assert lucas (4 ) == 7
107+ assert lucas (5 ) == 11
108+ assert lucas (6 ) == 18
109+ assert lucas (7 ) == 29
110+
111+ # test cases for sum_series
112+ assert sum_series (0 , 0 , 1 ) == 0 #fibonacci 1
113+ assert sum_series (1 , 0 , 1 ) == 1 #fibonacci 2
114+ assert sum_series (2 , 0 , 1 ) == 1 #fibonacci 3
115+ assert sum_series (3 , 0 , 1 ) == 2 #fibonacci 4
116+ assert sum_series (0 , 2 , 1 ) == 2 #lucas 1
117+ assert sum_series (1 , 2 , 1 ) == 1 #lucas 2
118+ assert sum_series (2 , 2 , 1 ) == 3 #lucas 3
119+ assert sum_series (3 , 2 , 1 ) == 4 #lucas 4
120+ assert sum_series (0 , 3 , 2 ) == 3 #other 4
121+ assert sum_series (1 , 3 , 2 ) == 2 #other 4
122+ assert sum_series (2 , 3 , 2 ) == 5 #other 4
123+ assert sum_series (3 , 3 , 2 ) == 7 #other 4
124+
125+ # throw a whole bunch of random stuff at sum_series
126+ for n in range (0 , 4 , 1 ):
127+ for firstValue in range (0 , 4 , 1 ):
128+ for secondValue in range (0 , 4 , 1 ):
129+ print n , firstValue , secondValue , sum_series (n , firstValue , secondValue )
130+
131+ print 'All Tests Passed'
0 commit comments