Skip to content

Commit 06e2b07

Browse files
committed
Merge pull request UWPCE-PythonCert#15 from imdavis/master
Ian M. Davis Homework Love those docstrings! Very nicely done. One thing -- it's not really worth catching the AssertionErrors -- they will get hit anyway, so not a lot of value added. But good form for Exceptions in general.
2 parents 2a80b6f + ab822ab commit 06e2b07

File tree

5 files changed

+241
-0
lines changed

5 files changed

+241
-0
lines changed

Students/imdavis/lightningTalk.pdf

442 KB
Binary file not shown.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# a function which takes on a single argument defining the size
2+
# of a grid to draw
3+
4+
def print_grid(gridsize):
5+
# define the top and bottom portion of a single grid cell
6+
corner = "+"
7+
midtopbot = 4 * "-"
8+
9+
# define the center of a single grid cell
10+
gridcenter = "|" + 4 * " "
11+
12+
# define how to draw the complete top, bottom, and middle of the
13+
# full grid in a single direction
14+
topbot = gridsize * (corner + midtopbot) + corner
15+
center = gridsize * gridcenter + "|"
16+
17+
# build the grid in one direction
18+
onedirgrid = topbot + "\n"
19+
onedirgrid += 4 * (center + "\n")
20+
21+
#build the full grid
22+
grid = gridsize * onedirgrid + topbot
23+
24+
# now print the grid
25+
print grid
26+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# draws a grid using the buildgrid function after prompting the
2+
# user for the size of the grid
3+
4+
import buildgrid
5+
6+
try:
7+
size = int(raw_input("What size grid would you like to build? "))
8+
buildgrid.print_grid(size)
9+
except ValueError:
10+
print "Please enter an integer!"

Students/imdavis/session02/ack.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python2.7
2+
# -*- coding: utf-8 -*-
3+
4+
"""Example of a recursive function with a good docstring.
5+
6+
For best results to view the docstring, after importing do:
7+
8+
>>> print ack.__doc__
9+
10+
Its value grows rapidly, even for small inputs. For example A(4,2) is an
11+
integer of 19,729 decimal digits. May hit maximum recursion limit. See:
12+
13+
sys.getrecursionlimit()
14+
sys.setrecursionlimit()
15+
16+
"""
17+
18+
def ack(m, n):
19+
"""Evaluation of the Ackermann Function.
20+
21+
The Ackermann Function is defined as:
22+
A(m, n) =
23+
n+1 if m = 0
24+
A(m−1, 1) if m > 0 and n = 0
25+
A(m−1, A(m, n−1)) if m > 0 and n > 0
26+
27+
Args:
28+
m (int): must be >= 0
29+
n (int): must be >= 0.
30+
31+
Yields:
32+
Evaluation of Ackermann function for A(m, n)
33+
34+
"""
35+
36+
if (m < 0 or n < 0):
37+
print "Arguments for the Ackermann function must be >= 0."
38+
return None
39+
elif (m == 0):
40+
return n + 1
41+
elif (n == 0):
42+
return ack(m - 1, 1)
43+
else:
44+
return ack(m - 1, ack(m, n - 1))
45+
46+
if __name__ == '__main__':
47+
TestsPass = True
48+
49+
try:
50+
assert ack(0, 0) == 1
51+
assert ack(0, 1) == 2
52+
assert ack(0, 2) == 3
53+
assert ack(0, 3) == 4
54+
assert ack(0, 4) == 5
55+
assert ack(1, 0) == 2
56+
assert ack(1, 1) == 3
57+
assert ack(1, 2) == 4
58+
assert ack(1, 3) == 5
59+
assert ack(1, 4) == 6
60+
assert ack(2, 0) == 3
61+
assert ack(2, 1) == 5
62+
assert ack(2, 2) == 7
63+
assert ack(2, 3) == 9
64+
assert ack(2, 4) == 11
65+
assert ack(3, 0) == 5
66+
assert ack(3, 1) == 13
67+
assert ack(3, 2) == 29
68+
assert ack(3, 3) == 61
69+
assert ack(3, 4) == 125
70+
assert ack(4, 0) == 13
71+
72+
except AssertionError:
73+
TestsPass = False
74+
print "All Tests Did Not Pass!"
75+
76+
if (TestsPass):
77+
print "All Tests Pass!"
78+
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env python2.7
2+
# -*- coding: utf-8 -*-
3+
4+
def fibonacci(n):
5+
"""Evaluation of the nth term of the Fibonnaci Series.
6+
7+
The Fibonnaci Series is defined as:
8+
0th term: 0
9+
1st term: 1
10+
nth term: sum of previous two terms in the series
11+
12+
Args:
13+
n (int): nth term in series (must be >= 0)
14+
15+
Yields:
16+
nth term of the Fibonnaci Series.
17+
18+
"""
19+
20+
if (n < 0):
21+
print "Arguments for the Fibonnaci series must be >= 0."
22+
return None
23+
elif (n == 0): # zeroth term = 0
24+
return 0
25+
elif (n == 1): # first term = 1
26+
return 1
27+
else:
28+
return fibonacci(n - 1) + fibonacci(n - 2)
29+
30+
def lucas(n):
31+
"""Evaluation of the nth term of the Lucas Series.
32+
33+
The Lucas Series is defined as:
34+
0th term: 2
35+
1st term: 1
36+
nth term: sum of previous two terms in the series
37+
38+
Args:
39+
n (int): nth term in series (must be >= 0)
40+
41+
Yields:
42+
nth term of the Lucas Series.
43+
44+
"""
45+
46+
if (n < 0):
47+
print "Arguments for the Lucas series must be >= 0."
48+
return None
49+
elif (n == 0): # zeroth term = 0
50+
return 2
51+
elif (n == 1): # first term = 1
52+
return 1
53+
else:
54+
return lucas(n - 1) + lucas(n - 2)
55+
56+
def sum_series(n, zerothTerm=0, firstTerm=1):
57+
"""Evaluation of the nth term of a series.
58+
59+
This function will return the nth term in a series which is the sum
60+
of the previous two consecutive terms in the series. The user can
61+
specify any zeroth and first terms to define the series, or can use
62+
the default values of 0 and 1 to give the n-th term of the
63+
Fibonacci series.
64+
65+
Args:
66+
n (int): nth term in series (must be >= 0)
67+
zerothTerm (int): zeroth term in the series (default = 0)
68+
firstTerm (int): first term in the series (default = 1)
69+
70+
Yields:
71+
nth term of the series.
72+
73+
"""
74+
75+
if (n < 0):
76+
print "Arguments for the series must be >= 0."
77+
return None
78+
elif (n == 0):
79+
return zerothTerm
80+
elif (n == 1):
81+
return firstTerm
82+
else:
83+
return sum_series(n - 1, zerothTerm, firstTerm) + sum_series(n - 2, zerothTerm, firstTerm)
84+
85+
86+
if __name__ == '__main__':
87+
FibanocciTestsPass = True
88+
LucasTestsPass = True
89+
ArbitrarySeriesTestsPass = True
90+
91+
# Some assertions to test the Fibonacci series function against known solutions
92+
try:
93+
assert fibonacci(0) == 0
94+
assert fibonacci(1) == 1
95+
assert fibonacci(10) == 55
96+
assert fibonacci(19) == 4181
97+
98+
except AssertionError:
99+
FibanocciTestsPass = False
100+
print "All Fibonacci Tests Did Not Pass!"
101+
102+
# Some assertions to test the Lucas series function against known solutions
103+
try:
104+
assert lucas(0) == 2
105+
assert lucas(1) == 1
106+
assert lucas(10) == 123
107+
assert lucas(20) == 15127
108+
109+
except AssertionError:
110+
LucasTestsPass = False
111+
print "All Lucas Tests Did Not Pass!"
112+
113+
# Some assertions to test the arbitrary series function against known solutions
114+
try:
115+
assert sum_series(10) == 55
116+
assert sum_series(10, 0, 1) == 55
117+
assert sum_series(10, 2, 1) == 123
118+
assert sum_series(15, 3, 3) == 2961
119+
assert sum_series(20, 5, 2) == 34435
120+
assert sum_series(20, 2, 5) == 42187
121+
122+
except AssertionError:
123+
ArbitrarySeriesTestsPass = False
124+
print "All aritrary series tests did not pass"
125+
126+
if (FibanocciTestsPass and LucasTestsPass and ArbitrarySeriesTestsPass):
127+
print "All Tests Pass!"

0 commit comments

Comments
 (0)