Skip to content

Commit 7387245

Browse files
committed
Merge pull request UWPCE-PythonCert#60 from ARSimmons/master
Initial commit
2 parents 6ce785b + 72120d2 commit 7387245

4 files changed

Lines changed: 181 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Git Primer
2+
3+
What is a graph?
4+
5+
>>A 'graph' - in the world of computer science,
6+
>>a state/place connected by paths.
7+
8+
What is an example of recurssive function?
9+
10+
>>
11+
12+
- ex using factorials
13+
14+
def factorial(x):
15+
if x == 1:
16+
return 1
17+
else:
18+
return x * factorial(x-1)
19+
20+
>>factorial(5)
21+
>>120
22+
23+
In math, the factorial of an integer is the
24+
result of multiplying that integer by every
25+
integer smaller than it down to 1.
26+
27+
5! == 5 * 4 * 3 * 2 * 1
28+
29+
30+
What is a module?
31+
32+
>> basically anything that ends in
33+
>> .py counts as a module
34+
35+
36+
What is a package?
37+
38+
>>IN ORDER For it to be a python package....needs
39+
>>an __init__.py
40+
41+
What is the HMK?
42+
43+
>> 1. Create a module called ack.py.
44+
>> put it in a session02 folder in your student
45+
>> folder. Write a function for the Ackerman
46+
>> function
47+
48+
>> 2. Fibronaci & Lucas functions
49+
>> and one that can do either
50+
51+
52+

‎Students/ASimmons/Session02/ack.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
__author__ = 'Ari'
2+
3+
4+
class ackerman:
5+
def ack(m,n):
6+
"""Return a non-negative integer based on the Ackerman function
7+
8+
Definition of Ackerman: http://en.wikipedia.org/wiki/Ackermann_function
9+
10+
m, n: non-negative integers"""
11+
12+
# validate that m is NOT a negative number
13+
if m < 0:
14+
return None
15+
16+
# validate that n is NOT a negative number
17+
if n < 0:
18+
return None
19+
20+
if m == 0:
21+
return n+1
22+
23+
if m > 0 and n == 0:
24+
return ack(m-1,1)
25+
26+
return ack(m-1, ack(m, n-1))
27+
28+
def main():
29+
30+
input_m = int(sys.argv[1])
31+
input_n = int(sys.argv[2])
32+
33+
ackerman.ack(input_m, input_n)
34+
35+
if __name__ == "__main__":
36+
main()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
__author__ = 'Ari'
2+
3+
def fibonacci(n):
4+
"""Return the nth value based on the Fibonacci sequence.
5+
For the purpose of this assignment I decided to start the
6+
sequence at 0"""
7+
current_num = 0
8+
prev_num = 1
9+
# first two values have already been set, thus n-2
10+
for i in range(0, n-2):
11+
temp = current_num
12+
current_num = prev_num
13+
prev_num= temp + prev_num
14+
return prev_num
15+
16+
def lucas(n):
17+
"""Return the nth value based on the Lucas sequence"""
18+
current_num = 2
19+
prev_num = 1
20+
# first two values have already been set, thus n-2
21+
for i in range(0, n-2):
22+
temp = current_num
23+
current_num = prev_num
24+
prev_num = temp + prev_num
25+
return prev_num
26+
27+
def sum_series(n, current_num=0, prev_num=1):
28+
"""Return the nth value of either the Fibonacci sequence (default) or the Lucas sequence."""
29+
# CURRENTLY this function doesn't work...returns nothing
30+
# I am not sure why, but my initial if statement
31+
# also is throwing syntax errors (when I inspect it
32+
# with the IDE
33+
if current_num=0 and prev_num=1:
34+
fibonacci(n)
35+
else:
36+
lucas(n)
37+
38+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
__author__ = 'Ari'
2+
3+
from ack import ackerman
4+
import unittest
5+
6+
# Note to Chris: So, I was experimenting with classes
7+
# and I've had this problem in the past where I try to run
8+
# test_somefunction and I get "You have 3 arguments, only need 2"
9+
# I'd really like to understand why this is (I can surmise it
10+
# is about the 'self'). Also what is 'unittest.TestCase'??
11+
12+
class test_ackerman(unittest.TestCase):
13+
14+
def test_ack(self):
15+
# coordinates 0,0
16+
test_func = ackerman()
17+
m = 0
18+
n = 0
19+
result = test_func.ack(m,n)
20+
assert_equal(result,1)
21+
22+
def test_ack(self):
23+
# coordinates 0,1
24+
test_func = ackerman()
25+
result = test_func.ack(0,1)
26+
assert_equal(result, 2)
27+
28+
def test_ack(self):
29+
# coordinates 1,2
30+
test_func = ackerman()
31+
result = test_func.ack(1,2)
32+
assert_equal(result, 4)
33+
34+
def test_ack(self):
35+
# coordinates 2,2
36+
test_func = ackerman()
37+
result = test_func.ack(2,2)
38+
assert_equal(result,7)
39+
40+
def test_ack(self):
41+
# coordinates 2,3
42+
test_func = ackerman()
43+
result = test_func.ack(2,3)
44+
assert_equal(result,9)
45+
46+
def test_ack(self):
47+
# coordinates 1,4
48+
test_func = ackerman()
49+
result = test_func.ack(1,4)
50+
assert_equal(result, 6)
51+
52+
53+
54+
55+

0 commit comments

Comments
 (0)