From be260c0d7005d683029562f666bea1d5474ace6d Mon Sep 17 00:00:00 2001 From: Anthony Date: Thu, 8 Oct 2020 16:40:52 -0500 Subject: [PATCH 1/2] initial commit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d113b2c51..9ab7235b2 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Module Project - Algorithms - +.............................. ## Sprint: Algorithms ## Modules: A First-Pass Solution and Writing Better Solutions From fba3ede2aceb96786ba26a90cb6b72d6b2ae2d81 Mon Sep 17 00:00:00 2001 From: Anthony Date: Thu, 8 Oct 2020 20:38:59 -0500 Subject: [PATCH 2/2] finished MVP --- README.md | 3 ++ eating_cookies/eating_cookies.py | 15 +++++++-- making_change/making_change.py | 33 +++++++++++-------- moving_zeroes/moving_zeroes.py | 21 +++++++++--- .../product_of_all_other_numbers.py | 7 ++-- .../test_product_of_all_other_numbers.py | 8 ++--- single_number/single_number.py | 5 ++- sliding_window_max/sliding_window_max.py | 19 +++++++---- 8 files changed, 74 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 9ab7235b2..3ae34b6e0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # Module Project - Algorithms .............................. +https://github.com/LambdaSchool/cs-module-project-algorithms/pull/116 +.............................. + ## Sprint: Algorithms ## Modules: A First-Pass Solution and Writing Better Solutions diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index aef505784..94fc6d791 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -3,9 +3,18 @@ Returns: an integer ''' def eating_cookies(n): - # Your code here - - pass + r = -1 + + if n < 0: + r = 0 + + elif n == 0: + r = 1 + + else: + r = eating_cookies(n-1) + eating_cookies(n-2) + eating_cookies(n-3) + return r + if __name__ == "__main__": # Use the main function here to test out your implementation diff --git a/making_change/making_change.py b/making_change/making_change.py index ae47fb338..eafa0935f 100644 --- a/making_change/making_change.py +++ b/making_change/making_change.py @@ -1,19 +1,26 @@ -#!/usr/bin/python - import sys -def making_change(amount, denominations): - # Your code here - pass +def eating_cookies(n, cache={0: 1, 1: 1, 2: 2}): + """ + n - int - Number of cookies in the jar + cache - dict - memoization of cookie methods already calculated + """ + + if n in cache: + return cache[n] + else: + cache[n] = eating_cookies(n - 3) + eating_cookies(n - 2) + eating_cookies(n - 1) + return cache[n] if __name__ == "__main__": - # Test our your implementation from the command line - # with `python making_change.py [amount]` with different amounts - if len(sys.argv) > 1: - denominations = [1, 5, 10, 25, 50] - amount = int(sys.argv[1]) - print("There are {ways} ways to make {amount} cents.".format(ways=making_change(amount, denominations), amount=amount)) - else: - print("Usage: making_change.py [amount]") \ No newline at end of file + if len(sys.argv) > 1: + num_cookies = int(sys.argv[1]) + print( + "There are {ways} ways for Cookie Monster to eat {n} cookies.".format( + ways=eating_cookies(num_cookies), n=num_cookies + ) + ) + else: + print("Usage: eating_cookies.py [num_cookies]") \ No newline at end of file diff --git a/moving_zeroes/moving_zeroes.py b/moving_zeroes/moving_zeroes.py index 2976ae48f..b16b067b1 100644 --- a/moving_zeroes/moving_zeroes.py +++ b/moving_zeroes/moving_zeroes.py @@ -2,14 +2,27 @@ Input: a List of integers Returns: a List of integers ''' -def moving_zeroes(arr): - # Your code here - pass +# function which pushes all zeros to the end of an array +def moving_zeroes(arr): + count = 0 + mock_arr = arr.copy() + + if len(arr) == 0: + return arr + + for i in mock_arr: + if i == 0: + arr.remove(i) + arr.append(i) + count += 1 + + return arr +# move all zeros present in the list to the end if __name__ == '__main__': # Use the main function here to test out your implementation arr = [0, 3, 1, 0, -2] - + print(f"The resulting of moving_zeroes is: {moving_zeroes(arr)}") \ No newline at end of file diff --git a/product_of_all_other_numbers/product_of_all_other_numbers.py b/product_of_all_other_numbers/product_of_all_other_numbers.py index 7d976257a..760d8a5f7 100644 --- a/product_of_all_other_numbers/product_of_all_other_numbers.py +++ b/product_of_all_other_numbers/product_of_all_other_numbers.py @@ -2,10 +2,9 @@ Input: a List of integers Returns: a List of integers ''' +from functools import reduce def product_of_all_other_numbers(arr): - # Your code here - - pass + return [ reduce(lambda x, y: x * y, arr[:i] + arr[i+1:]) for i in range(len(arr))] if __name__ == '__main__': @@ -13,4 +12,4 @@ def product_of_all_other_numbers(arr): # arr = [1, 2, 3, 4, 5] arr = [2, 6, 9, 8, 2, 2, 9, 10, 7, 4, 7, 1, 9, 5, 9, 1, 8, 1, 8, 6, 2, 6, 4, 8, 9, 5, 4, 9, 10, 3, 9, 1, 9, 2, 6, 8, 5, 5, 4, 7, 7, 5, 8, 1, 6, 5, 1, 7, 7, 8] - print(f"Output of product_of_all_other_numbers: {product_of_all_other_numbers(arr)}") + print(f"Output of product_of_all_other_numbers: {product_of_all_other_numbers(arr)}") \ No newline at end of file diff --git a/product_of_all_other_numbers/test_product_of_all_other_numbers.py b/product_of_all_other_numbers/test_product_of_all_other_numbers.py index bc6aceeb0..ad0f670f6 100644 --- a/product_of_all_other_numbers/test_product_of_all_other_numbers.py +++ b/product_of_all_other_numbers/test_product_of_all_other_numbers.py @@ -13,11 +13,11 @@ def test_product_of_all_other_numbers_division_friendly(self): self.assertEqual(product_of_all_other_numbers(arr), expected) # Uncomment this test to test your solution that doesn't utilize division - # def test_product_of_all_other_numbers_without_division(self): - # arr = [7, 9, 1, 8, 6, 0, 7, 8, 8, 7, 10] - # expected = [0, 0, 0, 0, 0, 94832640, 0, 0, 0, 0, 0] + def test_product_of_all_other_numbers_without_division(self): + arr = [7, 9, 1, 8, 6, 0, 7, 8, 8, 7, 10] + expected = [0, 0, 0, 0, 0, 94832640, 0, 0, 0, 0, 0] - # self.assertEqual(product_of_all_other_numbers(arr), expected) + self.assertEqual(product_of_all_other_numbers(arr), expected) if __name__ == '__main__': unittest.main() \ No newline at end of file diff --git a/single_number/single_number.py b/single_number/single_number.py index be6fd6278..1bb97eab5 100644 --- a/single_number/single_number.py +++ b/single_number/single_number.py @@ -3,9 +3,8 @@ Returns: an integer ''' def single_number(arr): - # Your code here - - pass + # applying the formula + return 2 * sum(set(arr)) - sum(arr) if __name__ == '__main__': diff --git a/sliding_window_max/sliding_window_max.py b/sliding_window_max/sliding_window_max.py index 518ed36a2..56349853c 100644 --- a/sliding_window_max/sliding_window_max.py +++ b/sliding_window_max/sliding_window_max.py @@ -2,15 +2,22 @@ Input: a List of integers as well as an integer `k` representing the size of the sliding window Returns: a List of integers ''' -def sliding_window_max(nums, k): - # Your code here - - pass - +def sliding_window_max(arr, k): + max = 0 + x = [] + for i in range(len(arr) - k + 1): + max = arr[i] + for j in range(1, k): + if arr[i + j] > max: + max = arr[i + j] + x.append(max) + + return x +# Driver method if __name__ == '__main__': # Use the main function here to test out your implementation arr = [1, 3, -1, -3, 5, 3, 6, 7] k = 3 - print(f"Output of sliding_window_max function is: {sliding_window_max(arr, k)}") + print(f"Output of sliding_window_max function is: {sliding_window_max(arr, k)}") \ No newline at end of file