Skip to content

Aaron's homework from lesson/session/module 2 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions students/aaron/homework_lesson2/fizzbuzz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#----------------------------------#
# Title: Fizz Buzz (from lesson 2)
# Changelog:
# Aaron Devey,2019-01-20,Created
#----------------------------------#


'''
fizzbuzz(n): return a number (as a str), or return Fizz, or Buzz, or FizzBuzz for multiples of 3, 5, or 15 respectively.
n: the number to return
returns: string
'''
def fizzbuzz(n):
if n % 15 == 0:
return("FizzBuzz")
if n % 5 == 0:
return("Buzz")
if n % 3 == 0:
return("Fizz")
return(str(n))


''' The instructions specifically want 1 through 100, so range(1, 101) is exactly that. '''
for n in range(1, 101):
print(fizzbuzz(n))

62 changes: 62 additions & 0 deletions students/aaron/homework_lesson2/grid_printer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#---------------------------------
# Title: grid_printer (from lesson 2)
# Changelog:
# Aaron Devey,2019-01-20,Created
#---------------------------------

''' Note: The definition of the side of a cell is re-defined in a different
way 3 separate times in this assignment. Since it's not consistent,
just trying to make a good approximation of what they're looking for
here based on part 3.
'''

# prints a seperator or bottom/top row
def print_seperator(cells, size):
line = ""
for i in range(cells):
line += "+" + " -" * size + " "
line += "+"
print(line)

# prints a line that forms a cell
def print_line(cells, size):
line = ""
for i in range(cells):
line += "|" + " " * size + " "
line += "|"
print(line)

# from part2, prints a 2x2 grid with cells of "size"
def print_grid(size):
for height in range(2):
print_seperator(2, size)
for lines in range(size):
print_line(2, size)
print_seperator(2, size)

# from part3, prints a grid of (cells)*(cells) and each cell sized at "size"
def print_grid2(cells, size):
for height in range(cells):
print_seperator(cells, size)
for i in range(size):
print_line(cells, size)
print_seperator(cells, size)

# from part1, prints a static grid
def static_grid():
print('''+ - - - - + - - - - +
| | |
| | |
| | |
| | |
+ - - - - + - - - - +
| | |
| | |
| | |
| | |
+ - - - - + - - - - +''')

static_grid()
print_grid(3)
print_grid(15)
print_grid2(5, 3)
10 changes: 10 additions & 0 deletions students/aaron/homework_lesson2/pushup4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#--------------------------------
# Title: diff21 (from python pushups 2 of 2)
# Changelog:
# Aaron Devey,2019-01-20,Created
#--------------------------------

def diff21(n):
if n > 21:
return((n - 21) * 2)
return(abs(n - 21))
10 changes: 10 additions & 0 deletions students/aaron/homework_lesson2/pushup5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#--------------------------------
# Title: parrot_trouble (from python pushups 2 of 2)
# Changelog:
# Aaron Devey,2019-01-20,Created
#--------------------------------

def parrot_trouble(talking, hour):
if talking and (hour > 20 or hour < 7):
return True
return False
12 changes: 12 additions & 0 deletions students/aaron/homework_lesson2/pushup6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#--------------------------------
# Title: makes10 (from python pushups 2 of 2)
# Changelog:
# Aaron Devey,2019-01-20,Created
#--------------------------------

def makes10(a, b):
if a + b == 10:
return True
if a == 10 or b == 10:
return True
return False
80 changes: 80 additions & 0 deletions students/aaron/homework_lesson2/series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#-----------------------------------#
# Title: series.py (from lesson 2)
# Changelog:
# Aaron Devey,2019-01-20,Created
#-----------------------------------#
'''
note: For each function I used a cache, for fun. I was noticing
that a large number of the recursive calls run multiple times,
and that the functions could be made more efficient with a cache.
'''


'''
fibonacci(n): calculate the Nth index of the fibonacci sequence
n: the desired index of the fibonacci squence
returns: int
'''
fibCache = {0: 0, 1: 1}
def fibonacci(n):
if n in fibCache:
return fibCache[n]
fibCache[n] = fibonacci(n - 1) + fibonacci(n - 2)
return fibCache[n]

'''
lucas(n): calculate the Nth index of the lucas sequence
n: the desired index of the lucas squence
returns: int
'''
lucasCache = {0: 2, 1: 1}
def lucas(n):
if n in lucasCache:
return lucasCache[n]
lucasCache[n] = lucas(n - 1) + lucas(n - 2)
return lucasCache[n]

'''
sum_series(n, [n0, [n1]]): calculate the Nth index of the fibonacci-like sequence
n: the desired index of the series
n0: the pre-defined index at 0
n1: the pre-defined index at 1
returns: int
note: defaults to the fibonacci sequence
'''
cache = {0: 0, 1: 1}
def sum_series(n, n0=0, n1=1):
# reset the cache if the starting values differ
if cache[0] != n0 or cache[1] != n1:
cache.clear()
cache[0] = n0
cache[1] = n1
if n in cache:
return cache[n]
cache[n] = sum_series(n - 1, n0, n1) + sum_series(n - 2, n0, n1)
return cache[n]


# tests
if __name__ == "__main__":
# run some tests
assert fibonacci(0) == 0
assert fibonacci(1) == 1
assert fibonacci(2) == 1
assert fibonacci(3) == 2
assert fibonacci(4) == 3
assert fibonacci(5) == 5
assert fibonacci(6) == 8
assert fibonacci(7) == 13

assert lucas(0) == 2
assert lucas(1) == 1

assert lucas(4) == 7

assert sum_series(5) == fibonacci(5)

# test if sum_series matched lucas
assert sum_series(5, 2, 1) == lucas(5)

print("tests passed")