Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Students/ASimmons/Session02/Session2_notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Git Primer

What is a graph?

>>A 'graph' - in the world of computer science,
>>a state/place connected by paths.

What is an example of recurssive function?

>>

- ex using factorials

def factorial(x):
if x == 1:
return 1
else:
return x * factorial(x-1)

>>factorial(5)
>>120

In math, the factorial of an integer is the
result of multiplying that integer by every
integer smaller than it down to 1.

5! == 5 * 4 * 3 * 2 * 1


What is a module?

>> basically anything that ends in
>> .py counts as a module


What is a package?

>>IN ORDER For it to be a python package....needs
>>an __init__.py

What is the HMK?

>> 1. Create a module called ack.py.
>> put it in a session02 folder in your student
>> folder. Write a function for the Ackerman
>> function

>> 2. Fibronaci & Lucas functions
>> and one that can do either



36 changes: 36 additions & 0 deletions Students/ASimmons/Session02/ack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
__author__ = 'Ari'


class ackerman:
def ack(m,n):
"""Return a non-negative integer based on the Ackerman function

Definition of Ackerman: http://en.wikipedia.org/wiki/Ackermann_function

m, n: non-negative integers"""

# validate that m is NOT a negative number
if m < 0:
return None

# validate that n is NOT a negative number
if n < 0:
return None

if m == 0:
return n+1

if m > 0 and n == 0:
return ack(m-1,1)

return ack(m-1, ack(m, n-1))

def main():

input_m = int(sys.argv[1])
input_n = int(sys.argv[2])

ackerman.ack(input_m, input_n)

if __name__ == "__main__":
main()
38 changes: 38 additions & 0 deletions Students/ASimmons/Session02/series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
__author__ = 'Ari'

def fibonacci(n):
"""Return the nth value based on the Fibonacci sequence.
For the purpose of this assignment I decided to start the
sequence at 0"""
current_num = 0
prev_num = 1
# first two values have already been set, thus n-2
for i in range(0, n-2):
temp = current_num
current_num = prev_num
prev_num= temp + prev_num
return prev_num

def lucas(n):
"""Return the nth value based on the Lucas sequence"""
current_num = 2
prev_num = 1
# first two values have already been set, thus n-2
for i in range(0, n-2):
temp = current_num
current_num = prev_num
prev_num = temp + prev_num
return prev_num

def sum_series(n, current_num=0, prev_num=1):
"""Return the nth value of either the Fibonacci sequence (default) or the Lucas sequence."""
# CURRENTLY this function doesn't work...returns nothing
# I am not sure why, but my initial if statement
# also is throwing syntax errors (when I inspect it
# with the IDE
if current_num=0 and prev_num=1:
fibonacci(n)
else:
lucas(n)


55 changes: 55 additions & 0 deletions Students/ASimmons/Session02/test_ack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
__author__ = 'Ari'

from ack import ackerman
import unittest

# Note to Chris: So, I was experimenting with classes
# and I've had this problem in the past where I try to run

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getting ahead of yourself!

# test_somefunction and I get "You have 3 arguments, only need 2"
# I'd really like to understand why this is (I can surmise it
# is about the 'self'). Also what is 'unittest.TestCase'??

class test_ackerman(unittest.TestCase):

def test_ack(self):
# coordinates 0,0
test_func = ackerman()
m = 0
n = 0
result = test_func.ack(m,n)
assert_equal(result,1)

def test_ack(self):
# coordinates 0,1
test_func = ackerman()
result = test_func.ack(0,1)
assert_equal(result, 2)

def test_ack(self):
# coordinates 1,2
test_func = ackerman()
result = test_func.ack(1,2)
assert_equal(result, 4)

def test_ack(self):
# coordinates 2,2
test_func = ackerman()
result = test_func.ack(2,2)
assert_equal(result,7)

def test_ack(self):
# coordinates 2,3
test_func = ackerman()
result = test_func.ack(2,3)
assert_equal(result,9)

def test_ack(self):
# coordinates 1,4
test_func = ackerman()
result = test_func.ack(1,4)
assert_equal(result, 6)