From 4e1a90fd3a5dc318a99c80921285d23144efb8f3 Mon Sep 17 00:00:00 2001 From: bcabanayan Date: Tue, 28 May 2019 20:41:58 -0700 Subject: [PATCH 01/10] Added solution for eating cookies --- eating_cookies/eating_cookies.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 62655d803..4b7f5f081 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -6,7 +6,17 @@ # a solution that is more efficient than the naive # recursive solution def eating_cookies(n, cache=None): - pass + if n < 0: + return 0 + elif n == 0: + return 1 + else: + #3 recursive calls + # n - 1 + # n - 2 + # n - 3 + return eating_cookies(n - 1) + eating_cookies(n - 2) + eating_cookies(n - 3) + return 0 if __name__ == "__main__": if len(sys.argv) > 1: From 4706b18df4f29d689425686dc0979d016fca6e6c Mon Sep 17 00:00:00 2001 From: bcabanayan Date: Wed, 29 May 2019 19:06:14 -0700 Subject: [PATCH 02/10] Added improved_eating_cookies function that uses caching --- eating_cookies/eating_cookies.py | 46 ++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 4b7f5f081..cae0c3362 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -5,6 +5,7 @@ # The cache parameter is here for if you want to implement # a solution that is more efficient than the naive # recursive solution + def eating_cookies(n, cache=None): if n < 0: return 0 @@ -16,11 +17,52 @@ def eating_cookies(n, cache=None): # n - 2 # n - 3 return eating_cookies(n - 1) + eating_cookies(n - 2) + eating_cookies(n - 3) - return 0 + 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]') + +def improved_eating_cookies(n, ways): + # base case, valid way + if n == 0: + return 1 + # base case, invalid way + elif n < 0: + return 0 + # result found, return without recalculating + elif n in ways: + return ways[n] + # result not found, had to calculate + else: + result = improved_eating_cookies(n - 1, ways) + improved_eating_cookies(n - 2, ways) + improved_eating_cookies(n - 3, ways) + ways[n] = result + return result + +print(improved_eating_cookies(3, {})) + +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]') + +def improved_eating_cookies(n, ways): + # base case, valid way + if n == 0: + return 1 + # base case, invalid way + elif n < 0: + return 0 + # result found, return without recalculating + elif n in ways: + return ways[n] + # result not found, had to calculate + else: + result = eating_cookies(n - 1) + eating_cookies(n - 2) + eating_cookies(n - 3) + ways[n] = result + return result From 50462ba203c0f896bfc1d1a83640e8ba8867d968 Mon Sep 17 00:00:00 2001 From: bcabanayan Date: Wed, 29 May 2019 20:17:13 -0700 Subject: [PATCH 03/10] Completed stock_prices exercise --- making_change/making_change.py | 4 +++- stock_prices/stock_prices.py | 23 +++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/making_change/making_change.py b/making_change/making_change.py index 9adad4470..6262a6caa 100644 --- a/making_change/making_change.py +++ b/making_change/making_change.py @@ -2,10 +2,12 @@ import sys +# can kind of follow same set up as eating cookies +# getting from an initial value to zero! + def making_change(amount, denominations): pass - if __name__ == "__main__": # Test our your implementation from the command line # with `python making_change.py [amount]` with different amounts diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index 9de20bc94..8c34acff3 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -3,13 +3,20 @@ import argparse def find_max_profit(prices): - pass + sorted_prices = sorted(prices) + max_profit_so_far = sorted_prices[0] - sorted_prices[-1] + for i in range(len(prices)): + current_min_price_so_far = prices[i] + for j in range(i + 1, len(prices)): + profit = prices[j] - current_min_price_so_far + if profit > max_profit_so_far: + max_profit_so_far = profit + return max_profit_so_far +# if __name__ == '__main__': +# # This is just some code to accept inputs from the command line +# parser = argparse.ArgumentParser(description='Find max profit from prices.') +# parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer price') +# args = parser.parse_args() -if __name__ == '__main__': - # This is just some code to accept inputs from the command line - parser = argparse.ArgumentParser(description='Find max profit from 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)) \ No newline at end of file From 52a444a2c423f2a49e688983c9aa9d39cd24dd11 Mon Sep 17 00:00:00 2001 From: bcabanayan Date: Wed, 29 May 2019 20:20:43 -0700 Subject: [PATCH 04/10] Added commenting to stock_prices --- stock_prices/stock_prices.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index 8c34acff3..632c883b8 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -3,12 +3,16 @@ import argparse def find_max_profit(prices): + # sort prices to find difference between highest and lowest price sorted_prices = sorted(prices) max_profit_so_far = sorted_prices[0] - sorted_prices[-1] + # find buy price for i in range(len(prices)): current_min_price_so_far = prices[i] + # find max profit by looping through each sell price in the array and subtract buy price from the sell price for j in range(i + 1, len(prices)): profit = prices[j] - current_min_price_so_far + # if profit is greater than current max profit, then update max profit value if profit > max_profit_so_far: max_profit_so_far = profit return max_profit_so_far From 6f85024e94964ca2fc227612c0f7bfc118313d13 Mon Sep 17 00:00:00 2001 From: bcabanayan Date: Wed, 29 May 2019 21:08:58 -0700 Subject: [PATCH 05/10] Completed recipe_batches exercise --- recipe_batches/recipe_batches.py | 9 +++++++-- stock_prices/stock_prices.py | 12 ++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/recipe_batches/recipe_batches.py b/recipe_batches/recipe_batches.py index c845950c5..b8dcf22ac 100644 --- a/recipe_batches/recipe_batches.py +++ b/recipe_batches/recipe_batches.py @@ -3,8 +3,13 @@ import math def recipe_batches(recipe, ingredients): - pass - + min_batch_values = [] + if set(recipe.keys()).issubset(set(ingredients.keys())): + for ingredient in ingredients: + min_batch_values.append(ingredients[ingredient]//recipe[ingredient]) + else: + return 0 + return min(min_batch_values) 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 632c883b8..593f29fbf 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -17,10 +17,10 @@ def find_max_profit(prices): max_profit_so_far = profit return max_profit_so_far -# if __name__ == '__main__': -# # This is just some code to accept inputs from the command line -# parser = argparse.ArgumentParser(description='Find max profit from prices.') -# parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer price') -# args = parser.parse_args() +if __name__ == '__main__': + # This is just some code to accept inputs from the command line + parser = argparse.ArgumentParser(description='Find max profit from 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)) \ No newline at end of file From 99e0e41e792036005f93e3b150946cccb511eeeb Mon Sep 17 00:00:00 2001 From: bcabanayan Date: Thu, 30 May 2019 19:23:37 -0700 Subject: [PATCH 06/10] Added list of all RPS plays and initialized final list --- rock_paper_scissors/rps.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rock_paper_scissors/rps.py b/rock_paper_scissors/rps.py index 0fc53356e..fe4796bc1 100644 --- a/rock_paper_scissors/rps.py +++ b/rock_paper_scissors/rps.py @@ -3,8 +3,12 @@ import sys def rock_paper_scissors(n): - pass + plays = [['rock'],['paper'],['scissors']] + final_list = [[]] + if n == 0: + return final_list +print(rock_paper_scissors(1)) if __name__ == "__main__": if len(sys.argv) > 1: From 8c07c019674d9c98a469ac92e608cc83959f7e3e Mon Sep 17 00:00:00 2001 From: bcabanayan Date: Sat, 1 Jun 2019 13:42:49 -0700 Subject: [PATCH 07/10] Completed solution for rock paper scissors --- rock_paper_scissors/rps.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/rock_paper_scissors/rps.py b/rock_paper_scissors/rps.py index fe4796bc1..fba85c70f 100644 --- a/rock_paper_scissors/rps.py +++ b/rock_paper_scissors/rps.py @@ -3,12 +3,23 @@ import sys def rock_paper_scissors(n): - plays = [['rock'],['paper'],['scissors']] - final_list = [[]] + final_list = [] + options = [['rock'],['paper'],['scissors']] + # def add_plays(plays_so_far): + # for i in plays_so_far: + # for j in plays: + # i = i + j + # return plays_so_far if n == 0: - return final_list - -print(rock_paper_scissors(1)) + return [[]] + else: + previous_plays = rock_paper_scissors(n - 1) + for previous_play in previous_plays: + for option in options: + final_list.append(previous_play + option) + return final_list + +print(rock_paper_scissors(2)) if __name__ == "__main__": if len(sys.argv) > 1: From f2a77aa12d9a8d8e25e87bdf6d703b965dfc1eaa Mon Sep 17 00:00:00 2001 From: bcabanayan Date: Sat, 1 Jun 2019 14:37:33 -0700 Subject: [PATCH 08/10] Removed unnecessary commenting from rps --- rock_paper_scissors/rps.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/rock_paper_scissors/rps.py b/rock_paper_scissors/rps.py index fba85c70f..56c3eda66 100644 --- a/rock_paper_scissors/rps.py +++ b/rock_paper_scissors/rps.py @@ -5,11 +5,6 @@ def rock_paper_scissors(n): final_list = [] options = [['rock'],['paper'],['scissors']] - # def add_plays(plays_so_far): - # for i in plays_so_far: - # for j in plays: - # i = i + j - # return plays_so_far if n == 0: return [[]] else: From 9c9da485c418f84928c10a66d08195f3b535404c Mon Sep 17 00:00:00 2001 From: bcabanayan Date: Sat, 1 Jun 2019 16:23:54 -0700 Subject: [PATCH 09/10] Added function to count number of permutations of denominations to making_change --- making_change/making_change.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/making_change/making_change.py b/making_change/making_change.py index 6262a6caa..0622dd43c 100644 --- a/making_change/making_change.py +++ b/making_change/making_change.py @@ -5,8 +5,24 @@ # can kind of follow same set up as eating cookies # getting from an initial value to zero! -def making_change(amount, denominations): - pass +# denominations = [1, 5, 10, 25, 50] + +def making_change(amount, denominations, ways): + if amount == 0: + return 1 + elif amount < 0: + return 0 + elif amount in ways: + return ways[amount] + else: + result = 0 + for i in range(len(denominations) - 1, -1, -1): + result = result + making_change(amount - denominations[i], denominations, ways) + ways[amount] = result + if result.sort() not in ways: + return result + +print(making_change(10, [1, 5, 10, 25, 50], {})) if __name__ == "__main__": # Test our your implementation from the command line From c47a5f0231c86529bb2538a54d3e99940e44ac65 Mon Sep 17 00:00:00 2001 From: bcabanayan Date: Sun, 2 Jun 2019 13:50:56 -0700 Subject: [PATCH 10/10] Updated solution to making_change to count combinations instead of permutations --- making_change/making_change.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/making_change/making_change.py b/making_change/making_change.py index 0622dd43c..e33060205 100644 --- a/making_change/making_change.py +++ b/making_change/making_change.py @@ -7,22 +7,17 @@ # denominations = [1, 5, 10, 25, 50] -def making_change(amount, denominations, ways): - if amount == 0: - return 1 - elif amount < 0: +def making_change(amount, denominations): + if amount < 0 or amount % 1 != 0: return 0 - elif amount in ways: - return ways[amount] - else: - result = 0 - for i in range(len(denominations) - 1, -1, -1): - result = result + making_change(amount - denominations[i], denominations, ways) - ways[amount] = result - if result.sort() not in ways: - return result - -print(making_change(10, [1, 5, 10, 25, 50], {})) + table = [0 for x in range(amount + 1)] + table[0] = 1 + for i in range(0,len(denominations)): + for j in range(denominations[i], amount + 1): + table[j] += table[j - denominations[i]] + return table[amount] + +print(making_change(10, [1, 5, 10, 25, 50])) if __name__ == "__main__": # Test our your implementation from the command line