diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 62655d803..7fa65322f 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -1,13 +1,30 @@ #!/usr/bin/python +# 0: eat 0 cookies 1 time +# 1: eat 1 cookie 1 time +# 2: eat 1 cookie 1 time +# 2: eat 2 cookies +# 3: eat 1 cookie 1 time, eat 1 cookie 1 time, eat 1 cookie 1 time + import sys # 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 + 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]) diff --git a/making_change/making_change.py b/making_change/making_change.py index 9adad4470..7670e629d 100644 --- a/making_change/making_change.py +++ b/making_change/making_change.py @@ -2,8 +2,24 @@ import sys +cache = {1:1} + def making_change(amount, denominations): - pass + + if amount in cache: + return cache[amount] + totalWays = 0 + + for d in denominations: + if d <= amount: + totalWays += making_change(amount-d, denominations) + making_change(amount, denominations) + + cache[amount] = totalWays + return totalWays + + print(cache) + print(making_change(10,[1, 5, 10, 25, 50])) + if __name__ == "__main__": diff --git a/recipe_batches/recipe_batches.py b/recipe_batches/recipe_batches.py index c845950c5..ca97b6749 100644 --- a/recipe_batches/recipe_batches.py +++ b/recipe_batches/recipe_batches.py @@ -3,7 +3,18 @@ import math def recipe_batches(recipe, ingredients): - pass + batch_size = None + for recipe_ing, recipe_cnt in recipe.items(): + if recipe_ing in ingredients: + ingredient_cnt = ingredients[recipe_ing] + maximum = int(ingredient_cnt / recipe_cnt) + if batch_size == None or maximum < batch_size: + batch_size = maximum + else: + return 0 + + return batch_size or 0 + if __name__ == '__main__': diff --git a/rock_paper_scissors/rps.py b/rock_paper_scissors/rps.py index 0fc53356e..b21001c44 100644 --- a/rock_paper_scissors/rps.py +++ b/rock_paper_scissors/rps.py @@ -2,8 +2,21 @@ import sys +import pprint def rock_paper_scissors(n): - pass + throws = ['rock', 'paper', 'scissors'] + + allPlays = [[]] + for i in range(n): + newPlays = [] + for play in allPlays: + # three times below: + for throw in throws: + newPlay = play + [throw] + newPlays.append(newPlay) + allPlays = newPlays + + return allPlays if __name__ == "__main__": diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index 9de20bc94..17f3aa2f8 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -3,8 +3,20 @@ import argparse def find_max_profit(prices): - pass - + lowest = prices[0] + highest = prices[0] + maximum = None + for price in prices[1:]: + diff = price - lowest + if maximum == None or diff > maximum: + maximum = diff + if price > highest: + highest = price + if price < lowest: + lowest = price + + return maximum + if __name__ == '__main__': # This is just some code to accept inputs from the command line