Skip to content
This repository was archived by the owner on Jul 18, 2020. It is now read-only.
Open

Cs28 #308

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
30 changes: 23 additions & 7 deletions eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,30 @@
import sys

# The cache parameter is here for if you want to implement
# a solution that is more efficient than the naive
# a solution that is more efficient than the naive
# recursive solution


def eating_cookies(n, cache=None):
pass
if n < 0:
return 0
if n == 0:
return 1
elif cache and cache[n] > 0:
return cache[n]
else:
if cache is None:
cache = {}
value = eating_cookies(
n - 1, cache) + eating_cookies(n - 2, cache) + eating_cookies(n - 3, cache)
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]')
29 changes: 23 additions & 6 deletions recipe_batches/recipe_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,30 @@

import math


def recipe_batches(recipe, ingredients):
pass
# sets number of batches and counter (to count the batches) to zero
max_batch = 0
counter = 0

# checks ingredients on hand vs needed by recipe
for i in recipe:
try:
# only whole numbers available
batches = ingredients[i] // recipe[i]
except:
batches = 0 # if not.....
if counter == 0 or batches <= max_batch:
# changes max_batch to how ever many batches you could make (from earlier in the function)
max_batch = batches
counter += 1
return max_batch


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))
36 changes: 30 additions & 6 deletions rock_paper_scissors/rps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,37 @@

import sys

# define valid moves.....no 'spock', 'monster' etc.
validMoves = [['rock'], ['paper'], ['scissors']]


def rock_paper_scissors(n):
pass
valid_plays = [["rock"], ["paper"], ["scissors"]]
# Base case: return empty list if n is zero
if n == 0:
return [[]]
# If n is 1 return valid_plays
if n == 1:
return valid_plays

# Create empty list to store results
output = []
# Recursively call function for rounds n
rounds = rock_paper_scissors(n - 1)
# For each round
for round in rounds:
# For each valid play
for play in valid_plays:
# Add new play to round in output
new_play = round + play
output.append(new_play)

return output


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]')
36 changes: 30 additions & 6 deletions stock_prices/stock_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,38 @@

import argparse


def find_max_profit(prices):
pass

if len(prices) == 1:
return 0

lowest = prices[0] # base for initial buying, 1st number in array
maxProfit = prices[1] - prices[0] # compare to get base max profit
index = 2

while True:
if index > len(prices): # if index is less than total length of array, exit
break
for price in prices[index-1:]:
# starting at beginning of array, compares initial ( or current) prices to next day to see profit margin,
# if larger than base maxProfit, make new maxProfit.
if price - lowest > maxProfit:
maxProfit = price - lowest
lowest = prices[index-1]
index += 1 # increment index to advance through array
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))