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
35 changes: 26 additions & 9 deletions eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@

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
cache = {}

if n in cache:
return cache[n]

if n == 0:
value = 1
elif n == 1:
value = 1
elif n == 2:
value = 2
elif n == 3:
value = 4
elif n > 3:
value = eating_cookies(n-3) + eating_cookies(n-2) + eating_cookies(n-1)

cache[n] = value
return value


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]')
33 changes: 16 additions & 17 deletions knapsack/knapsack.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
#!/usr/bin/python

import sys
from collections import namedtuple

Item = namedtuple('Item', ['index', 'size', 'value'])


def knapsack_solver(items, capacity):
pass
pass


if __name__ == '__main__':
if len(sys.argv) > 1:
capacity = int(sys.argv[2])
file_location = sys.argv[1].strip()
file_contents = open(file_location, 'r')
items = []
if len(sys.argv) > 1:
capacity = int(sys.argv[2])
file_location = sys.argv[1].strip()
file_contents = open(file_location, 'r')
items = []

for line in file_contents.readlines():
data = line.rstrip().split()
items.append(Item(int(data[0]), int(data[1]), int(data[2])))

for line in file_contents.readlines():
data = line.rstrip().split()
items.append(Item(int(data[0]), int(data[1]), int(data[2])))

file_contents.close()
print(knapsack_solver(items, capacity))
else:
print('Usage: knapsack.py [filename] [capacity]')
file_contents.close()
print(knapsack_solver(items, capacity))
else:
print('Usage: knapsack.py [filename] [capacity]')
22 changes: 11 additions & 11 deletions making_change/making_change.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/usr/bin/python

import sys


def making_change(amount, denominations):
pass
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]")
23 changes: 15 additions & 8 deletions recipe_batches/recipe_batches.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#!/usr/bin/python

import math


def recipe_batches(recipe, ingredients):
pass
min_ratio = math.inf
for ingredient, amount in recipe.items():
if ingredient not in ingredients:
return 0
ratio = math.floor(ingredients[ingredient] / amount)
if ratio < min_ratio:
min_ratio = ratio
return min_ratio


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, '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))
15 changes: 7 additions & 8 deletions rock_paper_scissors/rps.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/usr/bin/python

import sys


def rock_paper_scissors(n):
pass
pass


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]')
31 changes: 23 additions & 8 deletions stock_prices/stock_prices.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
#!/usr/bin/python

import argparse


def find_max_profit(prices):
pass

profits = []
maxProfit = 0

for i in range(len(prices)):
for j in range(i, len(prices)):
if prices[j] == prices[i]:
continue
else:
profit = prices[j] - prices[i]
profits.append(profit)

maxProfit = max(profits)
return maxProfit


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))