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
Binary file added Students/imdavis/lightningTalk.pdf
Binary file not shown.
26 changes: 26 additions & 0 deletions Students/imdavis/session01/buildgrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# a function which takes on a single argument defining the size
# of a grid to draw

def print_grid(gridsize):
# define the top and bottom portion of a single grid cell
corner = "+"
midtopbot = 4 * "-"
Copy link
Contributor

Choose a reason for hiding this comment

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

nice use of defined constants and string multiplication


# define the center of a single grid cell
gridcenter = "|" + 4 * " "

# define how to draw the complete top, bottom, and middle of the
# full grid in a single direction
topbot = gridsize * (corner + midtopbot) + corner
center = gridsize * gridcenter + "|"

# build the grid in one direction
onedirgrid = topbot + "\n"
onedirgrid += 4 * (center + "\n")

#build the full grid
grid = gridsize * onedirgrid + topbot

# now print the grid
print grid

10 changes: 10 additions & 0 deletions Students/imdavis/session01/drawgrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# draws a grid using the buildgrid function after prompting the
# user for the size of the grid

import buildgrid

try:
size = int(raw_input("What size grid would you like to build? "))
buildgrid.print_grid(size)
except ValueError:
Copy link
Contributor

Choose a reason for hiding this comment

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

jumping ahead to Exceptions, I see -- nicely done.

print "Please enter an integer!"
78 changes: 78 additions & 0 deletions Students/imdavis/session02/ack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

"""Example of a recursive function with a good docstring.

For best results to view the docstring, after importing do:

>>> print ack.__doc__

Its value grows rapidly, even for small inputs. For example A(4,2) is an
integer of 19,729 decimal digits. May hit maximum recursion limit. See:

sys.getrecursionlimit()
sys.setrecursionlimit()

"""

def ack(m, n):
"""Evaluation of the Ackermann Function.

The Ackermann Function is defined as:
A(m, n) =
n+1 if m = 0
A(m−1, 1) if m > 0 and n = 0
A(m−1, A(m, n−1)) if m > 0 and n > 0

Args:
m (int): must be >= 0
n (int): must be >= 0.

Yields:
Evaluation of Ackermann function for A(m, n)

"""

if (m < 0 or n < 0):
print "Arguments for the Ackermann function must be >= 0."
return None
elif (m == 0):
return n + 1
elif (n == 0):
return ack(m - 1, 1)
else:
return ack(m - 1, ack(m, n - 1))

if __name__ == '__main__':
TestsPass = True

try:
assert ack(0, 0) == 1
assert ack(0, 1) == 2
assert ack(0, 2) == 3
assert ack(0, 3) == 4
assert ack(0, 4) == 5
assert ack(1, 0) == 2
assert ack(1, 1) == 3
assert ack(1, 2) == 4
assert ack(1, 3) == 5
assert ack(1, 4) == 6
assert ack(2, 0) == 3
assert ack(2, 1) == 5
assert ack(2, 2) == 7
assert ack(2, 3) == 9
assert ack(2, 4) == 11
assert ack(3, 0) == 5
assert ack(3, 1) == 13
assert ack(3, 2) == 29
assert ack(3, 3) == 61
assert ack(3, 4) == 125
assert ack(4, 0) == 13

except AssertionError:
TestsPass = False
print "All Tests Did Not Pass!"

if (TestsPass):
print "All Tests Pass!"

127 changes: 127 additions & 0 deletions Students/imdavis/session02/series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

def fibonacci(n):
"""Evaluation of the nth term of the Fibonnaci Series.

The Fibonnaci Series is defined as:
0th term: 0
1st term: 1
nth term: sum of previous two terms in the series

Args:
n (int): nth term in series (must be >= 0)

Yields:
nth term of the Fibonnaci Series.

"""

if (n < 0):
print "Arguments for the Fibonnaci series must be >= 0."
return None
elif (n == 0): # zeroth term = 0
return 0
elif (n == 1): # first term = 1
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)

def lucas(n):
"""Evaluation of the nth term of the Lucas Series.

The Lucas Series is defined as:
0th term: 2
1st term: 1
nth term: sum of previous two terms in the series

Args:
n (int): nth term in series (must be >= 0)

Yields:
nth term of the Lucas Series.

"""

if (n < 0):
print "Arguments for the Lucas series must be >= 0."
return None
elif (n == 0): # zeroth term = 0
return 2
elif (n == 1): # first term = 1
return 1
else:
return lucas(n - 1) + lucas(n - 2)

def sum_series(n, zerothTerm=0, firstTerm=1):
"""Evaluation of the nth term of a series.

This function will return the nth term in a series which is the sum
of the previous two consecutive terms in the series. The user can
specify any zeroth and first terms to define the series, or can use
the default values of 0 and 1 to give the n-th term of the
Fibonacci series.

Args:
n (int): nth term in series (must be >= 0)
zerothTerm (int): zeroth term in the series (default = 0)
firstTerm (int): first term in the series (default = 1)

Yields:
nth term of the series.

"""

if (n < 0):
print "Arguments for the series must be >= 0."
return None
elif (n == 0):
return zerothTerm
elif (n == 1):
return firstTerm
else:
return sum_series(n - 1, zerothTerm, firstTerm) + sum_series(n - 2, zerothTerm, firstTerm)


if __name__ == '__main__':
FibanocciTestsPass = True
LucasTestsPass = True
ArbitrarySeriesTestsPass = True

# Some assertions to test the Fibonacci series function against known solutions
try:
assert fibonacci(0) == 0
assert fibonacci(1) == 1
assert fibonacci(10) == 55
assert fibonacci(19) == 4181

except AssertionError:
FibanocciTestsPass = False
print "All Fibonacci Tests Did Not Pass!"

# Some assertions to test the Lucas series function against known solutions
try:
assert lucas(0) == 2
assert lucas(1) == 1
assert lucas(10) == 123
assert lucas(20) == 15127

except AssertionError:
LucasTestsPass = False
print "All Lucas Tests Did Not Pass!"

# Some assertions to test the arbitrary series function against known solutions
try:
assert sum_series(10) == 55
assert sum_series(10, 0, 1) == 55
assert sum_series(10, 2, 1) == 123
assert sum_series(15, 3, 3) == 2961
assert sum_series(20, 5, 2) == 34435
assert sum_series(20, 2, 5) == 42187

except AssertionError:
ArbitrarySeriesTestsPass = False
print "All aritrary series tests did not pass"

if (FibanocciTestsPass and LucasTestsPass and ArbitrarySeriesTestsPass):
print "All Tests Pass!"