From 1c0b4611af72ed2cc71804b973aef6f17b393af7 Mon Sep 17 00:00:00 2001 From: Isvvc Date: Wed, 18 Mar 2020 11:17:49 -0600 Subject: [PATCH 01/12] Complete stock_prices --- stock_prices/stock_prices.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index 9de20bc94..ec3509657 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -3,7 +3,13 @@ import argparse def find_max_profit(prices): - pass + max_profit_neg = prices[0] - prices[1] + + for i in range(0, len(prices) - 1): + for j in range(i + 1, len(prices) - 1): + if prices[i] - prices[j] < max_profit_neg: + max_profit_neg = prices[i] - prices[j] + return max_profit_neg * -1 if __name__ == '__main__': @@ -12,4 +18,4 @@ def find_max_profit(prices): parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer price') args = parser.parse_args() - print("A profit of ${profit} can be made from the stock prices {prices}.".format(profit=find_max_profit(args.integers), prices=args.integers)) \ No newline at end of file + print("A profit of ${profit} can be made from the stock prices {prices}.".format(profit=find_max_profit(args.integers), prices=args.integers)) From 4feab0ab9e85a2d19b351be4b8bbf828df89f01c Mon Sep 17 00:00:00 2001 From: Isvvc Date: Wed, 18 Mar 2020 11:28:45 -0600 Subject: [PATCH 02/12] Complete recipe_batches --- recipe_batches/recipe_batches.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/recipe_batches/recipe_batches.py b/recipe_batches/recipe_batches.py index c845950c5..cd2568973 100644 --- a/recipe_batches/recipe_batches.py +++ b/recipe_batches/recipe_batches.py @@ -3,7 +3,16 @@ import math def recipe_batches(recipe, ingredients): - pass + max_batches = None + for key in recipe: + if not key in ingredients: + # if you don't have any of one ingredient, + # then you can't make any + return 0 + local_max = math.floor(ingredients[key] / recipe[key]) + if max_batches is None or local_max < max_batches: + max_batches = local_max + return max_batches if __name__ == '__main__': @@ -11,4 +20,4 @@ def recipe_batches(recipe, ingredients): # your implementation with different inputs recipe = { 'milk': 100, 'butter': 50, 'flour': 5 } ingredients = { 'milk': 132, 'butter': 48, 'flour': 51 } - print("{batches} batches can be made from the available ingredients: {ingredients}.".format(batches=recipe_batches(recipe, ingredients), ingredients=ingredients)) \ No newline at end of file + print("{batches} batches can be made from the available ingredients: {ingredients}.".format(batches=recipe_batches(recipe, ingredients), ingredients=ingredients)) From 1ceffbc821e547778ecd3f211c08d27dd7757588 Mon Sep 17 00:00:00 2001 From: Isvvc Date: Wed, 18 Mar 2020 13:08:47 -0600 Subject: [PATCH 03/12] Complete eating_cookies --- eating_cookies/eating_cookies.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 62655d803..4c6915ae4 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -6,11 +6,33 @@ # a solution that is more efficient than the naive # recursive solution def eating_cookies(n, cache=None): - pass + if cache is None: + cache = [0 for i in range(n + 1)] + + count = 0 + if n == 0: + # Hard-coded case + return 1 + for k in range(1, 4): + # Loop through 1, 2, and 3 (k) + if n > k: + # Eat k cookies + m = n - k + # Check the number of possibilities + # for the remaining cookies + if cache[m] == 0: + value = eating_cookies(m) + count += value + cache[m] = value + else: + count += cache[m] + elif n == k: + count += 1 + return count if __name__ == "__main__": 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 + print('Usage: eating_cookies.py [num_cookies]') From c2441e43987685bd36b96c7e520f589bad8d46ad Mon Sep 17 00:00:00 2001 From: Isvvc Date: Wed, 18 Mar 2020 14:52:01 -0600 Subject: [PATCH 04/12] Print the corrent number of rps plays but without correct hands --- rock_paper_scissors/rps.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/rock_paper_scissors/rps.py b/rock_paper_scissors/rps.py index 0fc53356e..937ca5cfd 100644 --- a/rock_paper_scissors/rps.py +++ b/rock_paper_scissors/rps.py @@ -3,7 +3,35 @@ import sys def rock_paper_scissors(n): - pass + rps = ['rock', 'paper', 'scissors'] + + # We need 3^(n-1) arrays starting with each hand + # 3^(n-2) arrays starting with hadn1, hand2 + # 3^(n-3) arrays starting with hand1, hand2, hand3 + # etc. + + def add_hands(n, arr): + if n == 0: + return arr + + # for the first iteration we should have an array of length 3^(n-1) + # for the first 3^(n-2) arrays of each hand, we need to add each hand + for i in range(0, len(rps)): + # first iteration: add rock to the first 3^(n-2) elements + # 3^(n-2) is also the length of the array / 3 + length = int(len(arr) / 3) + for j in range(length * i, length * (i + 1)): + arr[j] += [rps[i]] + + return add_hands(n - 1, arr) + + result = [] + # set up arrays with their first elements + for hand in rps: + newArrs = [[hand] for i in range(0, pow(3, n - 1))] + result += newArrs + add_hands(n - 1, newArrs) + return result if __name__ == "__main__": @@ -11,4 +39,4 @@ def rock_paper_scissors(n): num_plays = int(sys.argv[1]) print(rock_paper_scissors(num_plays)) else: - print('Usage: rps.py [num_plays]') \ No newline at end of file + print('Usage: rps.py [num_plays]') From f6bc2aa4f066b26f95a2f0537d91007bb4232a57 Mon Sep 17 00:00:00 2001 From: Isvvc Date: Wed, 18 Mar 2020 15:09:57 -0600 Subject: [PATCH 05/12] Complete rps --- rock_paper_scissors/rps.py | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/rock_paper_scissors/rps.py b/rock_paper_scissors/rps.py index 937ca5cfd..a513eff1d 100644 --- a/rock_paper_scissors/rps.py +++ b/rock_paper_scissors/rps.py @@ -10,27 +10,12 @@ def rock_paper_scissors(n): # 3^(n-3) arrays starting with hand1, hand2, hand3 # etc. - def add_hands(n, arr): - if n == 0: - return arr - - # for the first iteration we should have an array of length 3^(n-1) - # for the first 3^(n-2) arrays of each hand, we need to add each hand - for i in range(0, len(rps)): - # first iteration: add rock to the first 3^(n-2) elements - # 3^(n-2) is also the length of the array / 3 - length = int(len(arr) / 3) - for j in range(length * i, length * (i + 1)): - arr[j] += [rps[i]] - - return add_hands(n - 1, arr) - + if n == 0: + return [[]] result = [] - # set up arrays with their first elements for hand in rps: - newArrs = [[hand] for i in range(0, pow(3, n - 1))] - result += newArrs - add_hands(n - 1, newArrs) + children = rock_paper_scissors(n-1) + result += [[hand] + children[i] for i in range(0, pow(3, n - 1))] return result From 8ae50273dc7610bc6f938d595426bf04b0ba8624 Mon Sep 17 00:00:00 2001 From: Isvvc Date: Wed, 18 Mar 2020 15:52:20 -0600 Subject: [PATCH 06/12] Implement overshooting making_change --- making_change/making_change.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/making_change/making_change.py b/making_change/making_change.py index 9adad4470..97669869a 100644 --- a/making_change/making_change.py +++ b/making_change/making_change.py @@ -2,8 +2,31 @@ import sys -def making_change(amount, denominations): - pass +def making_change(amount, denominations, is_sorted = False): + # Should be similar to eating_cookies, but using + # the denominations array instead of range(1, 4) + # The order things are done in doesn't matter here, though + + # Sort so you'll always be decreasing in denomination + if not is_sorted: + denominations.sort(reverse=True) + + count = 0 + if amount == 0: + return 1 + for d in denominations: + if d == 1: + # If you reach pennies, you're done with this iteration + count += 1 + else: + for i in range(1, int(amount / d) + 1): + if amount > d * i: + newAmount = amount - d * i + newDenoms = [x for x in denominations if x != d] + count += making_change(newAmount, newDenoms, True) + elif amount == d * i: + count += 1 + return count if __name__ == "__main__": @@ -14,4 +37,4 @@ def making_change(amount, denominations): 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 + print("Usage: making_change.py [amount]") From d8ef6c2d1638816d0a397eedcf7eba277c10a54d Mon Sep 17 00:00:00 2001 From: Isvvc Date: Wed, 18 Mar 2020 15:55:58 -0600 Subject: [PATCH 07/12] Prevent change configurations counting twice --- making_change/making_change.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/making_change/making_change.py b/making_change/making_change.py index 97669869a..6bf3a738e 100644 --- a/making_change/making_change.py +++ b/making_change/making_change.py @@ -22,7 +22,7 @@ def making_change(amount, denominations, is_sorted = False): for i in range(1, int(amount / d) + 1): if amount > d * i: newAmount = amount - d * i - newDenoms = [x for x in denominations if x != d] + newDenoms = [x for x in denominations if x < d] count += making_change(newAmount, newDenoms, True) elif amount == d * i: count += 1 From e762e8ba32ad4e59ab96bf585735b1141e845275 Mon Sep 17 00:00:00 2001 From: Isvvc Date: Thu, 19 Mar 2020 13:36:00 -0600 Subject: [PATCH 08/12] Implement caching for eating cookies --- eating_cookies/eating_cookies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 4c6915ae4..ce75ecc24 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -21,7 +21,7 @@ def eating_cookies(n, cache=None): # Check the number of possibilities # for the remaining cookies if cache[m] == 0: - value = eating_cookies(m) + value = eating_cookies(m, cache) count += value cache[m] = value else: From 89e1d95024c605ec0b706dada9bd58da6fa6a889 Mon Sep 17 00:00:00 2001 From: Isvvc Date: Thu, 19 Mar 2020 14:01:08 -0600 Subject: [PATCH 09/12] Implement caching in making_change --- making_change/making_change.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/making_change/making_change.py b/making_change/making_change.py index 6bf3a738e..9ff77ce9f 100644 --- a/making_change/making_change.py +++ b/making_change/making_change.py @@ -2,7 +2,10 @@ import sys -def making_change(amount, denominations, is_sorted = False): +def making_change(amount, denominations, is_sorted = False, cache = None): + if cache is None: + cache = {} + # Should be similar to eating_cookies, but using # the denominations array instead of range(1, 4) # The order things are done in doesn't matter here, though @@ -23,7 +26,15 @@ def making_change(amount, denominations, is_sorted = False): if amount > d * i: newAmount = amount - d * i newDenoms = [x for x in denominations if x < d] - count += making_change(newAmount, newDenoms, True) + newDenomsTuple = tuple(newDenoms) + if not newDenomsTuple in cache: + cache[newDenomsTuple] = {} + if newAmount in cache[newDenomsTuple]: + count += cache[newDenomsTuple][newAmount] + else: + value = making_change(newAmount, newDenoms, True, cache) + count += value + cache[newDenomsTuple][newAmount] = value elif amount == d * i: count += 1 return count From bb6857aa2e2bc2d94dc7b816f5452ed8534ed3dd Mon Sep 17 00:00:00 2001 From: Isvvc Date: Thu, 19 Mar 2020 14:48:38 -0600 Subject: [PATCH 10/12] Clarify code in recipe_batches and stock_prices --- recipe_batches/recipe_batches.py | 2 +- stock_prices/stock_prices.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/recipe_batches/recipe_batches.py b/recipe_batches/recipe_batches.py index cd2568973..3db1a87b4 100644 --- a/recipe_batches/recipe_batches.py +++ b/recipe_batches/recipe_batches.py @@ -9,7 +9,7 @@ def recipe_batches(recipe, ingredients): # if you don't have any of one ingredient, # then you can't make any return 0 - local_max = math.floor(ingredients[key] / recipe[key]) + local_max = ingredients[key] // recipe[key] if max_batches is None or local_max < max_batches: max_batches = local_max return max_batches diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index ec3509657..5ad2e98b5 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -3,13 +3,13 @@ import argparse def find_max_profit(prices): - max_profit_neg = prices[0] - prices[1] + max_profit = prices[1] - prices[0] for i in range(0, len(prices) - 1): for j in range(i + 1, len(prices) - 1): - if prices[i] - prices[j] < max_profit_neg: - max_profit_neg = prices[i] - prices[j] - return max_profit_neg * -1 + if prices[j] - prices[i] > max_profit: + max_profit = prices[j] - prices[i] + return max_profit if __name__ == '__main__': From dd51b0717f0dd79a8348ff0cd2208c31f5f4528b Mon Sep 17 00:00:00 2001 From: Isvvc Date: Thu, 19 Mar 2020 15:22:06 -0600 Subject: [PATCH 11/12] Complete knapsack stretch goal Doesn't pass the tests however. This is because the order the test expects the items to be in is arbitrary, and this my results list is not always ordered in the same way. --- knapsack/knapsack.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/knapsack/knapsack.py b/knapsack/knapsack.py index a130284f1..bfa096842 100644 --- a/knapsack/knapsack.py +++ b/knapsack/knapsack.py @@ -2,12 +2,38 @@ import sys from collections import namedtuple +from operator import itemgetter Item = namedtuple('Item', ['index', 'size', 'value']) def knapsack_solver(items, capacity): - pass - + ratios = [] + for item in items: + ratio = item[2] / item[1] + ratios.append((item,ratio)) + ratios.sort(key = itemgetter(1), reverse = True) + + knapsack = [] + has_space = True + while has_space: + # repeat until there are no items left + # or there isn't space left for the lightest item + best_value = next((item for item in ratios if item[0][1] <= capacity), None) + if best_value is None: + has_space = False + else: + item = best_value[0] + ratio = best_value[1] + knapsack.append(item) + capacity -= item[1] + ratios.remove(best_value) + + result = {} + result['Value'] = sum([item[2] for item in knapsack]) + result['Chosen'] = [item[0] for item in knapsack] + + return result + if __name__ == '__main__': if len(sys.argv) > 1: @@ -23,4 +49,4 @@ def knapsack_solver(items, capacity): file_contents.close() print(knapsack_solver(items, capacity)) else: - print('Usage: knapsack.py [filename] [capacity]') \ No newline at end of file + print('Usage: knapsack.py [filename] [capacity]') From 9a408924651c3912daf960693761cc60a8f78514 Mon Sep 17 00:00:00 2001 From: Isvvc Date: Thu, 19 Mar 2020 16:52:12 -0600 Subject: [PATCH 12/12] Sort knapsack to pass tests --- knapsack/knapsack.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/knapsack/knapsack.py b/knapsack/knapsack.py index bfa096842..c672969c8 100644 --- a/knapsack/knapsack.py +++ b/knapsack/knapsack.py @@ -28,6 +28,8 @@ def knapsack_solver(items, capacity): capacity -= item[1] ratios.remove(best_value) + knapsack.sort(key = itemgetter(0)) + result = {} result['Value'] = sum([item[2] for item in knapsack]) result['Chosen'] = [item[0] for item in knapsack]