From ab133286a2e9f997a1224b1428e2ea4ade5c2464 Mon Sep 17 00:00:00 2001 From: dpancho Date: Wed, 19 Feb 2020 18:05:36 -0800 Subject: [PATCH 1/6] stock prices and recipe batches --- recipe_batches/recipe_batches.py | 29 +++++++++++++++++++++++------ stock_prices/stock_prices.py | 29 +++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/recipe_batches/recipe_batches.py b/recipe_batches/recipe_batches.py index c845950c5..ce1e387cb 100644 --- a/recipe_batches/recipe_batches.py +++ b/recipe_batches/recipe_batches.py @@ -2,13 +2,30 @@ import math + def recipe_batches(recipe, ingredients): - pass + # sets number of batches and counter (to count the batches) to zero + max_batch = 0 + counter = 0 + + # checks ingredients on hand vs needed by recipe + for i in recipe: + try: + # only whole numbers available + batches = ingredients[i] // recipe[i] + except: + batches = 0 # if not..... + if counter == 0 or batches <= max_batch: + # changes max_batch to how ever many batches you could make (from earlier in the function) + max_batch = batches + counter += 1 + return max_batch if __name__ == '__main__': - # Change the entries of these dictionaries to test - # 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 + # Change the entries of these dictionaries to test + # 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)) diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index 9de20bc94..e3a7c1030 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -2,14 +2,31 @@ import argparse + def find_max_profit(prices): - pass + + lowest = prices[0] + maxProfit = prices[1] - prices[0] + index = 2 + + while True: + if index > len(prices): + break + for price in prices[index-1:]: + if price - lowest > maxProfit: + maxProfit = price - lowest + lowest = prices[index-1] + index += 1 + return maxProfit 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() + # 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)) From 511da082bb2a2e7c69832760c1d0c2a13bb18cba Mon Sep 17 00:00:00 2001 From: dpancho Date: Thu, 20 Feb 2020 14:17:06 -0800 Subject: [PATCH 2/6] stock prices notes update --- stock_prices/stock_prices.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index e3a7c1030..fcb31d147 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -5,18 +5,20 @@ def find_max_profit(prices): - lowest = prices[0] - maxProfit = prices[1] - prices[0] + lowest = prices[0] # base for initial buying, 1st number in array + maxProfit = prices[1] - prices[0] # compare to get base max profit index = 2 while True: - if index > len(prices): + if index > len(prices): # if index is less than total length of array, exit break for price in prices[index-1:]: + # starting at beginning of array, compares initial ( or current) prices to next day to see profit margin, + # if larger than base maxProfit, make new maxProfit. if price - lowest > maxProfit: maxProfit = price - lowest lowest = prices[index-1] - index += 1 + index += 1 # increment index to advance through array return maxProfit From 31dfcc174fe77f0abcc7ca655d3c58a35c3d611e Mon Sep 17 00:00:00 2001 From: dpancho Date: Thu, 20 Feb 2020 17:15:09 -0800 Subject: [PATCH 3/6] cookies done, rps not working YET. ignore it --- eating_cookies/eating_cookies.py | 30 +++++++++++++++++++++++------- rock_paper_scissors/rps.py | 27 +++++++++++++++++++++------ 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 62655d803..ecabc1ee3 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -3,14 +3,30 @@ import sys # The cache parameter is here for if you want to implement -# a solution that is more efficient than the naive +# 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 + elif cache and cache[n] > 0: + return cache[n] + else: + if cache is None: + cache = {} + value = eating_cookies( + n - 1, cache) + eating_cookies(n - 2, cache) + eating_cookies(n - 3, cache) + cache[n] = value + return value + 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 + 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]') diff --git a/rock_paper_scissors/rps.py b/rock_paper_scissors/rps.py index 0fc53356e..3c3ac98a2 100644 --- a/rock_paper_scissors/rps.py +++ b/rock_paper_scissors/rps.py @@ -2,13 +2,28 @@ import sys +# define valid moves.....no 'spock', 'monster' etc. +validMoves = [['rock'], ['paper'], ['scissors']] + + def rock_paper_scissors(n): - pass + if n == 0: + return [[]] + if n == 1: + return validMoves + + output = [] + arr = rock_paper_scissors(n - 1) + for subArr in arr: + for move in validMoves: + newMove = subArr + move + output.append(newMove) + return output if __name__ == "__main__": - if len(sys.argv) > 1: - 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 + if len(sys.argv) > 1: + num_plays = int(sys.argv[1]) + print(rock_paper_scissors(num_plays)) + else: + print('Usage: rps.py [num_plays]') From 2d0422ebc0f8ce3d1f99fcd507ddec7cd4e109be Mon Sep 17 00:00:00 2001 From: dpancho Date: Thu, 20 Feb 2020 18:04:09 -0800 Subject: [PATCH 4/6] Rock, paper scissors for the win. MVP --- rock_paper_scissors/rps.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/rock_paper_scissors/rps.py b/rock_paper_scissors/rps.py index 3c3ac98a2..eecd73733 100644 --- a/rock_paper_scissors/rps.py +++ b/rock_paper_scissors/rps.py @@ -7,18 +7,27 @@ def rock_paper_scissors(n): + valid_plays = [["rock"], ["paper"], ["scissors"]] + # Base case: return empty list if n is zero if n == 0: return [[]] + # If n is 1 return valid_plays if n == 1: - return validMoves - - output = [] - arr = rock_paper_scissors(n - 1) - for subArr in arr: - for move in validMoves: - newMove = subArr + move - output.append(newMove) - return output + return valid_plays + + # Create empty list to store results + output = [] + # Recursively call function for rounds n + rounds = rock_paper_scissors(n - 1) + # For each round + for round in rounds: + # For each valid play + for play in valid_plays: + # Add new play to round in output + new_play = round + play + output.append(new_play) + + return output if __name__ == "__main__": From 628ba06528e96e2902dc5bf3d27e93bd15207cf3 Mon Sep 17 00:00:00 2001 From: dpancho Date: Thu, 16 Apr 2020 01:08:17 -0700 Subject: [PATCH 5/6] changes for cs28 --- stock_prices/stock_prices.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index fcb31d147..c7adc3d08 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -32,3 +32,5 @@ def find_max_profit(prices): print("A profit of ${profit} can be made from the stock prices {prices}.".format( profit=find_max_profit(args.integers), prices=args.integers)) + + From 7878a5827fdc35bd8111729a7f0d1c0f28cd1da8 Mon Sep 17 00:00:00 2001 From: dpancho Date: Thu, 16 Apr 2020 14:46:44 -0700 Subject: [PATCH 6/6] updated with new case --- stock_prices/stock_prices.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index c7adc3d08..d2752427f 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -5,6 +5,9 @@ def find_max_profit(prices): + if len(prices) == 1: + return 0 + lowest = prices[0] # base for initial buying, 1st number in array maxProfit = prices[1] - prices[0] # compare to get base max profit index = 2 @@ -33,4 +36,4 @@ def find_max_profit(prices): print("A profit of ${profit} can be made from the stock prices {prices}.".format( profit=find_max_profit(args.integers), prices=args.integers)) - +