Skip to content

Commit 91e47cd

Browse files
Carolyn.EvansCarolyn.Evans
authored andcommitted
Initial checkin session01 session02 homework
1 parent 85d7f9e commit 91e47cd

4 files changed

Lines changed: 285 additions & 0 deletions

File tree

.DS_Store

-6 KB
Binary file not shown.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# This was my first attempt.
2+
# This one prints the number of boxes horizontal and vertical based on the 'x' parameter,
3+
# and the size of each box is determined by the 'x' parameter as well.
4+
def grid1(x):
5+
6+
for a in range(0,x,1):
7+
8+
for b in range(0,x,1):
9+
10+
print '+',
11+
12+
for c in range(0,x,1):
13+
print '-',
14+
15+
print '+'
16+
17+
for b in range(0,x,1):
18+
19+
for c in range(0,x,1):
20+
21+
print '|',
22+
23+
for a in range(0,x,1):
24+
print ' ',
25+
26+
print '|'
27+
28+
for b in range(0,x,1):
29+
30+
print '+',
31+
32+
for c in range(0,x,1):
33+
print '-',
34+
35+
print '+'
36+
37+
grid1(4)
38+
39+
# This was my second attempt.
40+
# This one also prints the number of boxes horizontal and vertical based on the 'size' parameter,
41+
# and the size of each box is determined by the 'size' parameter as well.
42+
# The difference is that the code has been simplified by calling a sub-function.
43+
def grid2(size):
44+
45+
for a in range(0,size,1):
46+
47+
lineDraw2(size,'+','-')
48+
49+
for b in range(0,size,1):
50+
lineDraw2(size,'|',' ')
51+
52+
lineDraw2(size,'+', '-')
53+
54+
def lineDraw2 (size,border,filler):
55+
for a in range(0,size,1):
56+
57+
print border,
58+
59+
for b in range(0,size,1):
60+
print filler,
61+
62+
print border
63+
64+
grid2(4)
65+
66+
67+
# Then I read the homework assignment more thoroughly.
68+
# This one prints a grid with two vertical and two horizontal boxes.
69+
# The size of whole entire grid is determined by the 'size' parameter.
70+
def grid3(size):
71+
72+
size -= 1
73+
size /= 2
74+
size -= 1
75+
76+
for outerLoop in range(0,2,1):
77+
78+
lineDraw3(size,'+','-')
79+
80+
for innerLoop in range(0,size,1):
81+
lineDraw3(size,'|',' ')
82+
83+
lineDraw3(size,'+', '-')
84+
85+
def lineDraw3 (size,border,filler):
86+
for outerLoop in range(0,2,1):
87+
88+
print border,
89+
90+
for innerLoop in range(0,size,1):
91+
print filler,
92+
93+
print border
94+
95+
grid3(13)
96+
97+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
__author__ = 'carolyn.evans'
2+
3+
def ack(m, n):
4+
""" This is the Ackermann Function.
5+
For a complete description, visit en.wikipedia.org/wiki/Ackermann_function.
6+
7+
:param m: Parameter m must be an integer value of zero or more.
8+
:param n: Parameter m must be an integer value of zero or more.
9+
:return: If m and n are valid, an integer is returned. Otherwise, None is returned.
10+
"""
11+
12+
import sys
13+
14+
try:
15+
if m < 0 or n < 0:
16+
return None
17+
18+
if m == 0:
19+
return n+1
20+
21+
if n == 0:
22+
return ack(m-1, 1)
23+
24+
return ack(m-1, ack(m, n-1))
25+
except Exception, err:
26+
sys.stderr.write('ERROR: %s\n' % str(err))
27+
return None
28+
29+
if __name__ == "__main__":
30+
assert ack(0, 0) == 1
31+
assert ack(0, 1) == 2
32+
assert ack(0, 2) == 3
33+
assert ack(0, 3) == 4
34+
assert ack(0, 4) == 5
35+
36+
assert ack(1, 0) == 2
37+
assert ack(1, 1) == 3
38+
assert ack(1, 2) == 4
39+
assert ack(1, 3) == 5
40+
assert ack(1, 4) == 6
41+
42+
assert ack(2, 0) == 3
43+
assert ack(2, 1) == 5
44+
assert ack(2, 2) == 7
45+
assert ack(2, 3) == 9
46+
assert ack(2, 4) == 11
47+
48+
assert ack(3, 0) == 5
49+
assert ack(3, 1) == 13
50+
assert ack(3, 2) == 29
51+
assert ack(3, 3) == 61
52+
assert ack(3, 4) == 125
53+
54+
assert ack(4, 0) == 13
55+
56+
57+
print 'All Tests Passed'
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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

Comments
 (0)