From c57ad07d7b0573169b205c72255fb1223459df4e Mon Sep 17 00:00:00 2001 From: Dorothy Gasque Date: Wed, 15 Apr 2020 16:03:21 -0700 Subject: [PATCH 1/2] update solution to stock_prices.py, recipe_batches.py, and partial solution to eating_cookies.py --- eating_cookies/eating_cookies.py | 6 +++++- recipe_batches/recipe_batches.py | 10 ++++++++-- stock_prices/stock_prices.py | 11 ++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 62655d803..8ce82664c 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -6,7 +6,11 @@ # a solution that is more efficient than the naive # recursive solution def eating_cookies(n, cache=None): - pass + if n < 0: + return 0 + if n == 0: + return 1 + return eating_cookies(n-1) + eating_cookies(n-2) + eating_cookies(n-3) if __name__ == "__main__": if len(sys.argv) > 1: diff --git a/recipe_batches/recipe_batches.py b/recipe_batches/recipe_batches.py index c845950c5..d127f8530 100644 --- a/recipe_batches/recipe_batches.py +++ b/recipe_batches/recipe_batches.py @@ -3,8 +3,14 @@ import math def recipe_batches(recipe, ingredients): - pass - + max_batch = 1000 + for key, value in recipe.items(): + if key not in ingredients.keys() or value > ingredients[key]: + return 0 + pos_batch = math.floor(ingredients[key]/value) + if pos_batch < max_batch: + max_batch = pos_batch + return max_batch if __name__ == '__main__': # Change the entries of these dictionaries to test diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index 9de20bc94..587c7d387 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -3,7 +3,16 @@ import argparse def find_max_profit(prices): - pass + cur_min = prices[0] + cur_max_profit = prices[1] - prices[0] + for i, x in enumerate(prices[:-1]): + if cur_min > x: + cur_min = x + for y in prices[i+1:]: + profit = y - cur_min + if profit > cur_max_profit: + cur_max_profit = profit + return cur_max_profit if __name__ == '__main__': From 2be23fa0c589c395fbe67fed8d64b785fe8b5aa8 Mon Sep 17 00:00:00 2001 From: Dorothy Gasque Date: Wed, 15 Apr 2020 17:20:08 -0700 Subject: [PATCH 2/2] Update eating_cookies.py Now works on large numbers --- eating_cookies/eating_cookies.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 8ce82664c..4c314c708 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -6,11 +6,19 @@ # a solution that is more efficient than the naive # recursive solution def eating_cookies(n, cache=None): + if cache is None: + cache = [0 for i in range(n+1)] if n < 0: return 0 - if n == 0: + elif n >= 0 and n <= 1: return 1 - return eating_cookies(n-1) + eating_cookies(n-2) + eating_cookies(n-3) + elif cache[n] != 0: + return cache[n] + else: + answer = eating_cookies(n-1, cache) + eating_cookies(n-2, cache) + eating_cookies(n-3, cache) + cache[n] = answer + + return answer if __name__ == "__main__": if len(sys.argv) > 1: