From 91cfdea2b76b8bb0f297ea370e26cd9c58b444dc Mon Sep 17 00:00:00 2001 From: A D Date: Sun, 20 Jan 2019 17:45:04 -0800 Subject: [PATCH 1/4] Adding series.py from session/module/lesson number 2 --- students/aaron/homework_lesson2/series.py | 80 +++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 students/aaron/homework_lesson2/series.py diff --git a/students/aaron/homework_lesson2/series.py b/students/aaron/homework_lesson2/series.py new file mode 100644 index 0000000..6acac8f --- /dev/null +++ b/students/aaron/homework_lesson2/series.py @@ -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") From 2977d6a98919a8f27a724bfa7e315c07de9f16c0 Mon Sep 17 00:00:00 2001 From: A D Date: Sun, 20 Jan 2019 17:55:40 -0800 Subject: [PATCH 2/4] Adding fizzbuzz homework from session/module/lesson number 2 --- students/aaron/homework_lesson2/fizzbuzz.py | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 students/aaron/homework_lesson2/fizzbuzz.py diff --git a/students/aaron/homework_lesson2/fizzbuzz.py b/students/aaron/homework_lesson2/fizzbuzz.py new file mode 100644 index 0000000..bbc9957 --- /dev/null +++ b/students/aaron/homework_lesson2/fizzbuzz.py @@ -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)) + From 893bb0e6fa69880ef955f17c52909e62c8f0ac27 Mon Sep 17 00:00:00 2001 From: A D Date: Sun, 20 Jan 2019 18:23:25 -0800 Subject: [PATCH 3/4] Adding grid_printer homework from session/module/lesson number 2 --- .../aaron/homework_lesson2/grid_printer.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 students/aaron/homework_lesson2/grid_printer.py diff --git a/students/aaron/homework_lesson2/grid_printer.py b/students/aaron/homework_lesson2/grid_printer.py new file mode 100644 index 0000000..370aab4 --- /dev/null +++ b/students/aaron/homework_lesson2/grid_printer.py @@ -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) From cf055eb56b57aa141c81034d7fd8456f95c290ab Mon Sep 17 00:00:00 2001 From: A D Date: Sun, 20 Jan 2019 18:34:27 -0800 Subject: [PATCH 4/4] Adding python pushups 4 through 6 from session/module/lesson number 2 --- students/aaron/homework_lesson2/pushup4.py | 10 ++++++++++ students/aaron/homework_lesson2/pushup5.py | 10 ++++++++++ students/aaron/homework_lesson2/pushup6.py | 12 ++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 students/aaron/homework_lesson2/pushup4.py create mode 100644 students/aaron/homework_lesson2/pushup5.py create mode 100644 students/aaron/homework_lesson2/pushup6.py diff --git a/students/aaron/homework_lesson2/pushup4.py b/students/aaron/homework_lesson2/pushup4.py new file mode 100644 index 0000000..abbd450 --- /dev/null +++ b/students/aaron/homework_lesson2/pushup4.py @@ -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)) diff --git a/students/aaron/homework_lesson2/pushup5.py b/students/aaron/homework_lesson2/pushup5.py new file mode 100644 index 0000000..c5442db --- /dev/null +++ b/students/aaron/homework_lesson2/pushup5.py @@ -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 diff --git a/students/aaron/homework_lesson2/pushup6.py b/students/aaron/homework_lesson2/pushup6.py new file mode 100644 index 0000000..c24960a --- /dev/null +++ b/students/aaron/homework_lesson2/pushup6.py @@ -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