Skip to content

Commit 8e4a75c

Browse files
author
smerdykov
committed
Putting finishing touches on series.py, adding docstrings/comments
1 parent 41d7797 commit 8e4a75c

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

‎Students/SSchwafel/session02/ack.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
print "n = " + str(n)
2828

2929
def is_negative(x):
30-
30+
"""This function checks to make sure that values are positive"""
3131
if x < 0:
3232

3333
x = raw_input("Ackermann's function is not defined for values less than 0. Please start over with a value that is greater than 0. \n\nExiting.\n")
@@ -39,7 +39,7 @@ def is_negative(x):
3939

4040

4141
def ackermann(x,y):
42-
42+
"""Performs Ackermann's function on values x = m, y = n"""
4343
#x = m
4444
#y = n
4545

‎Students/SSchwafel/session02/series.py‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/usr/bin/python
22

33

4-
user_number = raw_input('Which term do you want from the Fibonacci or Lucas sequence?\n')
4+
user_number = raw_input('Which term do you want from the Fibonacci or Lucas sequence?\n\n(be sure to ask for 0 if you want the first term from the sequence)\n\n')
55

66
user_number = int(user_number)
7+
print
78

89
def fibonacci(n):
910
"""Return the nth value of the Fibonacci sequence of numbers"""
@@ -33,24 +34,50 @@ def lucas(n):
3334
#print fibonacci(user_number)
3435

3536
def sum_series(n, optional1 = 0, optional2 = 1):
37+
"""This function takes one required argument and two optional ones.
38+
39+
If the optional arguments are 2,1, return nth Lucas number, otherwise, return nth Fibonacci number. If other arguments are given, return none until other series can be programmed.
40+
"""
3641

3742
if optional1 == 0 and optional2 == 1:
3843
return fibonacci(n)
39-
if optional1 == 2 and optional2 == 1:
44+
elif optional1 == 2 and optional2 == 1:
4045
return lucas(n)
46+
else:
47+
"It looks like you want a different series"
4148

4249

50+
#Return Lucas value of user_number
51+
print 'This is the Lucas number corresponding to the term you gave\n'
4352
print sum_series(user_number,2,1)
44-
45-
# This is where I am going to put my tests and asserts
53+
print
54+
print 'This is the Fibonacci number corresponding to the term you gave\n'
55+
print sum_series(user_number,0,1)
56+
print
4657

4758
if __name__ == "__main__":
4859

60+
#check to see if the Fibonacci numbers returned match the first 4 known values
4961
assert fibonacci(0) == 0
5062
assert fibonacci(1) == 1
5163
assert fibonacci(3) == 2
5264
assert fibonacci(5) == 5
5365

66+
#check to see if the Lucas numbers returned match the first few known values (and the 6th for good measure)
67+
68+
assert lucas(0) == 2
69+
assert lucas(1) == 1
70+
assert lucas(3) == 4
71+
assert lucas(5) == 11
72+
73+
#make sure that the sum_series function performs Fibonacci as expected, and Lucas as expected when given the necessary arguments
74+
75+
assert sum_series(0) == 0
76+
assert sum_series(0,0,1) == 0
77+
assert sum_series(5,0,1) == 5
78+
assert sum_series(1,2,1) == 1
79+
assert sum_series(5,2,1) == 11
80+
assert sum_series(0) == 0
81+
82+
5483
print "\nAll Tests Pass!\n"
55-
# #
56-
# #

0 commit comments

Comments
 (0)