diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 62655d803..cae0c3362 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -5,12 +5,64 @@ # 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): - 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) + + +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 = 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]') \ 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 = eating_cookies(n - 1) + eating_cookies(n - 2) + eating_cookies(n - 3) + ways[n] = result + return result diff --git a/making_change/making_change.py b/making_change/making_change.py index 9adad4470..e33060205 100644 --- a/making_change/making_change.py +++ b/making_change/making_change.py @@ -2,9 +2,22 @@ import sys -def making_change(amount, denominations): - pass +# can kind of follow same set up as eating cookies +# getting from an initial value to zero! + +# denominations = [1, 5, 10, 25, 50] +def making_change(amount, denominations): + if amount < 0 or amount % 1 != 0: + return 0 + 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 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/rock_paper_scissors/rps.py b/rock_paper_scissors/rps.py index 0fc53356e..56c3eda66 100644 --- a/rock_paper_scissors/rps.py +++ b/rock_paper_scissors/rps.py @@ -3,8 +3,18 @@ import sys def rock_paper_scissors(n): - pass - + final_list = [] + options = [['rock'],['paper'],['scissors']] + if n == 0: + 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: diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index 9de20bc94..593f29fbf 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -3,8 +3,19 @@ import argparse def find_max_profit(prices): - pass - + # 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 if __name__ == '__main__': # This is just some code to accept inputs from the command line