diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 62655d803..2ec440499 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -5,8 +5,25 @@ # 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 +def eating_cookies(n): + cache=[1, 1, 2] + if n == 0: + return cache[0] + if n == 1: + return cache[1] + if n == 2: + return cache[2] + if n == 3: + return sum(cache) + + for i in range(3, n): + cache.append(sum(cache)) + del cache[0] + + return sum(cache) + + + if __name__ == "__main__": if len(sys.argv) > 1: diff --git a/eating_cookies/test_eating_cookies.py b/eating_cookies/test_eating_cookies.py index 1a4644786..23065cd4b 100644 --- a/eating_cookies/test_eating_cookies.py +++ b/eating_cookies/test_eating_cookies.py @@ -11,9 +11,9 @@ def test_eating_cookies_small_n(self): self.assertEqual(eating_cookies(10), 274) def test_eating_cookies_large_n(self): - self.assertEqual(eating_cookies(50, [0 for i in range(51)]), 10562230626642) - self.assertEqual(eating_cookies(100, [0 for i in range(101)]), 180396380815100901214157639) - self.assertEqual(eating_cookies(500, [0 for i in range(501)]), 1306186569702186634983475450062372018715120191391192207156664343051610913971927959744519676992404852130396504615663042713312314219527) + self.assertEqual(eating_cookies(50), 10562230626642) + self.assertEqual(eating_cookies(100), 180396380815100901214157639) + self.assertEqual(eating_cookies(500), 1306186569702186634983475450062372018715120191391192207156664343051610913971927959744519676992404852130396504615663042713312314219527) if __name__ == '__main__': diff --git a/knapsack/knapsack.py b/knapsack/knapsack.py index a130284f1..1ff438b94 100644 --- a/knapsack/knapsack.py +++ b/knapsack/knapsack.py @@ -6,8 +6,24 @@ Item = namedtuple('Item', ['index', 'size', 'value']) def knapsack_solver(items, capacity): - pass - + weight = 0 + value = 0 + chosen = [] + item_dict = {} + for i in items: + item_dict[i[0]] = {'size': i[1], 'value': i[2], 'ratio': i[2] / i[1]} + + sorted_items = sorted(item_dict, key=lambda x: (item_dict[x]['ratio'], item_dict[x]['size'])) + sorted_items.reverse() + + for i in sorted_items: + if item_dict[i]['size'] + weight <= capacity: + weight += item_dict[i]['size'] + value += item_dict[i]['value'] + chosen.append(i) + chosen.sort() + return {'Value': value, 'Chosen': chosen} + if __name__ == '__main__': if len(sys.argv) > 1: diff --git a/recipe_batches/recipe_batches.py b/recipe_batches/recipe_batches.py index c845950c5..f8c8be56c 100644 --- a/recipe_batches/recipe_batches.py +++ b/recipe_batches/recipe_batches.py @@ -3,7 +3,14 @@ import math def recipe_batches(recipe, ingredients): - pass + batches = [] + for key in recipe.keys(): + if key not in ingredients.keys(): + print(2) + return 0 + batches.append(ingredients[key] // recipe[key]) + + return min(batches) if __name__ == '__main__': diff --git a/rock_paper_scissors/rps.py b/rock_paper_scissors/rps.py index 0fc53356e..e97b5eaed 100644 --- a/rock_paper_scissors/rps.py +++ b/rock_paper_scissors/rps.py @@ -3,7 +3,16 @@ import sys def rock_paper_scissors(n): - pass + rps = ['rock', 'paper', 'scissors'] # The three options + num_games = 3 ** n # How many game will be played + games = [[0] * n for x in range(num_games)] # Create list of lists with 0 place holders for each player + dividers = [3 ** x for x in range(0, n + 1)] + for i in range(0, len(games)): + for j in range(1, len(games[0]) + 1): + games[i][-j] = rps[(i // dividers[j - 1]) % 3] + + + return games if __name__ == "__main__": diff --git a/stock_prices/README.md b/stock_prices/README.md index 75d1af9e9..3bc4c0630 100644 --- a/stock_prices/README.md +++ b/stock_prices/README.md @@ -1,10 +1,10 @@ # Stock Prices -You want to write a bot that will automate the task of day-trading for you while you're going through Lambda. You decide to have your bot just focus on buying and selling Amazon stock. +You want to write a bot that will automate the task of day-trading for you while you're going through Lambda. You decide to have your bot just focus on buying and selling Amazon stock. Write a function `find_max_profit` that receives as input a list of stock prices. Your function should return the maximum profit that can be made from a single buy and sell. You must buy first before selling; no shorting is allowed here. -For example, `find_max_profit([1050, 270, 1540, 3800, 2])` should return 3530, which is the maximum profit that can be made from a single buy and then sell of these stock prices. +For example, `find_max_profit([1050, 270, 1540, 3800, 2])` should return 3530, which is the maximum profit that can be made from a single buy and then sell of these stock prices. ## Testing @@ -14,6 +14,6 @@ You can also test your implementation manually by executing `python stock_prices ## Hints - For this problem, we essentially want to find the maximum difference between the smallest and largest prices in the list of prices, but we also have to make sure that the max profit is computed by subtracting some price by another price that comes _before_ it; it can't come after it in the list of prices. + For this problem, we essentially want to find the maximum difference between the smallest and largest prices in the list of prices, but we also have to make sure that the max profit is computed by subtracting some price by another price that comes _before_ it; it can't come after it in the list of prices. - So what if we kept track of the `current_min_price_so_far` and the `max_profit_so_far`? \ No newline at end of file + So what if we kept track of the `current_min_price_so_far` and the `max_profit_so_far`? \ No newline at end of file diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index 9de20bc94..0918ed7c6 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -2,14 +2,29 @@ import argparse + def find_max_profit(prices): - pass + prices.reverse() + i = 0 + profit = None + while i < len(prices) - 1: + for j in prices[i+1:]: + if profit == None: + profit = prices[i] - j + else: + profit = max(profit, prices[i] - j) + i += 1 + + return profit 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))