Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions eating_cookies/test_eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down
20 changes: 18 additions & 2 deletions knapsack/knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion recipe_batches/recipe_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down
11 changes: 10 additions & 1 deletion rock_paper_scissors/rps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
8 changes: 4 additions & 4 deletions stock_prices/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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`?
So what if we kept track of the `current_min_price_so_far` and the `max_profit_so_far`?
27 changes: 21 additions & 6 deletions stock_prices/stock_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
print("A profit of ${profit} can be made from the stock prices {prices}.".format(
profit=find_max_profit(args.integers), prices=args.integers))