diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 62655d803..24d1a7467 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -6,7 +6,27 @@ # a solution that is more efficient than the naive # recursive solution def eating_cookies(n, cache=None): - pass + #base cases + if n < 0: + return 0 + + elif n == 0: + return 1 + +# if cache exist and contains the answer + elif cache and cache[n] > 0: + return cache[n] + + else: + # does cache exist? if not ccreate a cache + if not cache: + cache = {i: 0 for i in range(n + 1)} + + cache[n] = eating_cookies(n - 1, cache) + eating_cookies(n - 2, cache) + eating_cookies(n - 3, cache) + return cache[n] + + + if __name__ == "__main__": if len(sys.argv) > 1: diff --git a/recipe_batches/recipe_batches.py b/recipe_batches/recipe_batches.py index c845950c5..81da27599 100644 --- a/recipe_batches/recipe_batches.py +++ b/recipe_batches/recipe_batches.py @@ -2,10 +2,31 @@ import math -def recipe_batches(recipe, ingredients): - pass +""" +function parameters recipe = {}, ingredients = {} +ingredient { + name: amount +} +recipe { + ingredient_name: amt available +} + +return max + +""" + +def recipe_batches(recipe, ingredients): + for key in recipe.keys(): + if key in ingredients: + recipe[key] = ingredients[key] // recipe[key] + if recipe[key] == 0: + break + else: + recipe[key] = 0 + return min(recipe.values()) + if __name__ == '__main__': # Change the entries of these dictionaries to test # your implementation with different inputs diff --git a/rock_paper_scissors/rps.py b/rock_paper_scissors/rps.py index 0fc53356e..0fc0c2dd1 100644 --- a/rock_paper_scissors/rps.py +++ b/rock_paper_scissors/rps.py @@ -1,9 +1,30 @@ #!/usr/bin/python +""" +output =[] +first pass +output=[[rock], [paper], [scissors]] +second pass +output=[[rock, rock], [rock,paper], [rock,scissors], [paper, rock], [paper, paper], [ + paper,scissors], [scissors,rock], [scissors,paper], [scissors,scissors]] +base case accounts for the number of plays +in recursive calls to append to the inner lists + +""" import sys + def rock_paper_scissors(n): - pass + rps = ['rock', 'paper', 'scissors'] + plays = [] + def rock_paper_scissors_helper(options,n): + if n == 0: + plays.append(options) + else: + for item in rps: + rock_paper_scissors_helper(options + [item], n - 1 ) + rock_paper_scissors_helper([],n) + return plays if __name__ == "__main__": @@ -11,4 +32,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]') diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index 9de20bc94..aaa59def6 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -1,9 +1,41 @@ #!/usr/bin/python +""" +bot buys and sells amazon stocks +function find_max_profit (prices) + prices = [] + +return max_profit from a single buy and sell +order of trade: buy first before selling + +diff btw smallest and largest prices +max_profit = some price - another price that comes before it + +current_min_price_so_far = +max_profit_so_far = + +Algorithm +profit = [] +iterate through stock prices +keep track of lowest price +subtract lowest price from current item(i) +append result of subttaction to profit list +find the maximum profit using max() method + + +""" import argparse def find_max_profit(prices): - pass + profit = [] + for i in range (0, len(prices)): + for j in range (i + 1, len(prices)): + price_diff = prices[j] - prices[i] + profit.append(price_diff) + + return max(profit) + + if __name__ == '__main__':