Skip to content
56 changes: 54 additions & 2 deletions eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]')
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
17 changes: 15 additions & 2 deletions making_change/making_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

import sys

def making_change(amount, denominations):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work completing the making change exercise! Here is my solution:

def making_change(amount, denominations):
  cache = { 0: 1 }

  for a in range(denominations[0], amount + 1):
    cache[a] = cache[a - denominations[0]]

  for b in range(denominations[1], amount + 1):
    cache[b] = cache[b] + cache[b - denominations[1]]

  for c in range(denominations[2], amount + 1):
    cache[c] = cache[c] + cache[c - denominations[2]]

  for d in range(denominations[3], amount + 1):
    cache[d] = cache[d] + cache[d - denominations[3]]

  for e in range(denominations[4], amount + 1):
    cache[e] = cache[e] + cache[e - denominations[4]]
  
  return cache[amount] 

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
Expand Down
9 changes: 7 additions & 2 deletions recipe_batches/recipe_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import math

def recipe_batches(recipe, ingredients):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work completing the recipe batches exercises! Here is my solution for the recipe batches problem:

def recipe_batches(recipe, ingredients):
  min = None

  for item, amount in recipe.items():
    if item not in ingredients:
      return 0

    val = ingredients[item] // amount

    if val == 0:
      return 0
    elif min == None or val < min:
      min = val
  
  return min 

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
Expand Down
14 changes: 12 additions & 2 deletions rock_paper_scissors/rps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
import sys

def rock_paper_scissors(n):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work completing the rock, paper, and scissors exercise! Here is my solution:

def rock_paper_scissors(n):
  if n == 0:
    return [[]]

  choices = ['rock', 'paper', 'scissors']
  main = []

  def helper_func(main, start, end, copy):
    if start == end:
      return

    new_cpy = copy[:]

    for x in range(2):
      helper_func(main, start + 1, n, new_cpy)
      
      if new_cpy[start] == 'rock':
        new_cpy[start] = 'paper'
      elif new_cpy[start] == 'paper':
        new_cpy[start] = 'scissors'
        main.append(new_cpy[:])
        helper_func(main, start + 1, n, new_cpy)
        break
      else:
        new_cpy[start] = 'rock'
      
      main.append(new_cpy[:])

  for choice in choices:
    copy = [choice] + ['rock'] * (n - 1)
    main.append(copy)
    helper_func(main, 1, n, copy)

  return main

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:
Expand Down
15 changes: 13 additions & 2 deletions stock_prices/stock_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the instructions, no sorting is allowed for this problem.

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
Expand Down