Skip to content
This repository was archived by the owner on Jul 18, 2020. It is now read-only.
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
33 changes: 27 additions & 6 deletions eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,36 @@

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 cache is None:
cache = [0 for i in range(n + 1)]
total_ways = 0
if cache[n] != 0:
return cache[n]
if n == 0:
new_ways = 1
total_ways += new_ways
if n >= 1:
new_ways = eating_cookies(n - 1, cache)
total_ways += new_ways
if n >= 2:
new_ways = eating_cookies(n - 2, cache)
total_ways += new_ways
if n >= 3:
new_ways = eating_cookies(n - 3, cache)
total_ways += new_ways
cache[n] = total_ways
return total_ways


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]')
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]')
70 changes: 60 additions & 10 deletions making_change/making_change.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
#!/usr/bin/python

def making_change(amount, denominations):

return dynamic_get_combinations(amount, denominations)
# if you can subtract down to 0, return 1. Because that's one possibility
# if it's < 0 return 0 because it doesn't match
# for coin in array of coins, subtract the amount of the coin from the remaining and add the result to possible ways


def get_combinations(amount, denominations, index=0):
total_ways = 0

if amount == 0:
return 1
if amount < 0:
return 0

for current_index in range(index, len(denominations)):
new_amount = amount - denominations[current_index]
total_ways += get_combinations(new_amount, denominations, current_index)

return total_ways

def dynamic_get_combinations(amount, denominations):
combinations_array = [0 for number in range(amount + 1)]
combinations_array[0] = 1
for index in range(len(denominations)):
for sub_index in range(len(combinations_array)):
denomination = denominations[index]
if sub_index >= denomination:
combinations_array[sub_index] += combinations_array[sub_index - denomination]

return combinations_array[amount]


import sys
"""
20
10
0
10
5
5
"""



print(dynamic_get_combinations(12, [1, 2, 5]))#, 10, 25, 50]))

# if that combo.sort() not in dict, increase count




def making_change(amount, denominations):
pass


if __name__ == "__main__":
# Test our your implementation from the command line
# with `python making_change.py [amount]` with different amounts
if len(sys.argv) > 1:
denominations = [1, 5, 10, 25, 50]
amount = int(sys.argv[1])
print("There are {ways} ways to make {amount} cents.".format(ways=making_change(amount, denominations), amount=amount))
else:
print("Usage: making_change.py [amount]")
# Test our your implementation from the command line
# with `python making_change.py [amount]` with different amounts
if len(sys.argv) > 1:
denominations = [1, 5, 10, 25, 50]
amount = int(sys.argv[1])
print("There are {ways} ways to make {amount} cents.".format(ways=making_change(amount, denominations),
amount=amount))
else:
print("Usage: making_change.py [amount]")
20 changes: 14 additions & 6 deletions recipe_batches/recipe_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

import math

# run time: O(n)
def recipe_batches(recipe, ingredients):
pass
batches = []
for item in recipe:
if item not in ingredients:
return 0
batches.append(ingredients[item] // recipe[item])
return min(batches)


if __name__ == '__main__':
# Change the entries of these dictionaries to test
# your implementation with different inputs
recipe = { 'milk': 100, 'butter': 50, 'flour': 5 }
ingredients = { 'milk': 132, 'butter': 48, 'flour': 51 }
print("{batches} batches can be made from the available ingredients: {ingredients}.".format(batches=recipe_batches(recipe, ingredients), ingredients=ingredients))
# Change the entries of these dictionaries to test
# your implementation with different inputs
recipe = { 'milk': 100, 'butter': 50, 'cheese': 10 }
ingredients = { 'milk': 198, 'butter': 52, 'cheese': 10 }
print("{batches} batches can be made from the available ingredients: {ingredients}.".format(
batches=recipe_batches(recipe, ingredients), ingredients=ingredients))

70 changes: 64 additions & 6 deletions rock_paper_scissors/rps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,71 @@

import sys


def add_plays(plays):
possible_plays = ["rock", "paper", "scissors"]

if len(plays) <= 1:
return plays
one_third = len(plays) // 3
first = plays[:one_third]
second = plays[one_third:one_third * 2]
third = plays[one_third * 2:]
for item in first:
item.append(possible_plays[0])
for item in second:
item.append(possible_plays[1])
for item in third:
item.append(possible_plays[2])

first = add_plays(first)
second = add_plays(second)
third = add_plays(third)

return first + second + third


def rock_paper_scissors(n):
pass

# if n <= 0:
# return [[]]

outside_length = 3 ** n

possible_plays = [[] for combination in range(outside_length)]
return add_plays(possible_plays)


if __name__ == "__main__":
if len(sys.argv) > 1:
num_plays = int(sys.argv[1])
print(rock_paper_scissors(num_plays))
else:
print('Usage: rps.py [num_plays]')
if len(sys.argv) > 1:
num_plays = int(sys.argv[1])
print(rock_paper_scissors(num_plays))
else:
print('Usage: rps.py [num_plays]')

# [['rock', 'rock'], ['rock', 'paper'], ['rock', 'scissors'],
# ['paper', 'rock'], ['paper', 'paper'], ['paper', 'scissors'],
# ['scissors', 'rock'], ['scissors', 'paper'], ['scissors', 'scissors']]

# r, p, s
# rr, rp, rs
# pr, pp, ps
# sr, sp, ss
# rrr, rrp, rrs, rpr, rpp, rps, rsr, rsp, rss,
# etc

#
# [['rock', 'rock', 'rock', 'rock', 'rock'],
# ['paper', 'paper', 'rock', 'paper', 'paper'],
# ['scissors', 'scissors', 'scissors', 'rock', 'scissors']], \
# [['rock', 'paper', 'rock', 'rock', 'rock'],
# ['paper', 'paper', 'paper', 'paper', 'paper'],
# ['scissors', 'scissors', 'scissors', 'paper', 'scissors']], \
# [['rock', 'scissors', 'rock', 'rock', 'rock'],
# ['paper', 'paper', 'scissors', 'paper', 'paper'],
# ['scissors', 'scissors', 'scissors', 'scissors', 'scissors']]
#
# ([['rock'], ['rock'],
# ['rock']], [['paper'],
# ['paper'], ['paper']],
# [['scissors'], ['scissors'], ['scissors']])
Loading