diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 62655d803..69b0f0e59 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -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]') \ No newline at end of file + 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]') diff --git a/knapsack/knapsack.py b/knapsack/knapsack.py index a130284f1..cbe00e6ea 100644 --- a/knapsack/knapsack.py +++ b/knapsack/knapsack.py @@ -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]') \ No newline at end of file + file_contents.close() + print(knapsack_solver(items, capacity)) + else: + print('Usage: knapsack.py [filename] [capacity]') diff --git a/making_change/making_change.py b/making_change/making_change.py index 9adad4470..ed96800ee 100644 --- a/making_change/making_change.py +++ b/making_change/making_change.py @@ -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]") \ No newline at end of file + # 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]") diff --git a/recipe_batches/recipe_batches.py b/recipe_batches/recipe_batches.py index c845950c5..e37f12d5a 100644 --- a/recipe_batches/recipe_batches.py +++ b/recipe_batches/recipe_batches.py @@ -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)) \ No newline at end of file + # 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)) diff --git a/rock_paper_scissors/rps.py b/rock_paper_scissors/rps.py index 0fc53356e..48fd3c08f 100644 --- a/rock_paper_scissors/rps.py +++ b/rock_paper_scissors/rps.py @@ -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]') \ No newline at end of file + if len(sys.argv) > 1: + num_plays = int(sys.argv[1]) + print(rock_paper_scissors(num_plays)) + else: + print('Usage: rps.py [num_plays]') diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index 9de20bc94..c569e2637 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -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)) \ No newline at end of file + print("A profit of ${profit} can be made from the stock prices {prices}.".format( + profit=find_max_profit(args.integers), prices=args.integers))