From bb2da5aa7431ef56d7a7ad70f12a42d53ecbc762 Mon Sep 17 00:00:00 2001 From: Elizabeth Ter Sahakyan Date: Thu, 19 Mar 2020 11:08:53 -0700 Subject: [PATCH 1/3] stock prices test passed --- Pipfile | 11 +++++++++++ stock_prices/stock_prices.py | 25 ++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 Pipfile diff --git a/Pipfile b/Pipfile new file mode 100644 index 000000000..b9ba84f67 --- /dev/null +++ b/Pipfile @@ -0,0 +1,11 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] + +[dev-packages] + +[requires] +python_version = "3.7" diff --git a/stock_prices/stock_prices.py b/stock_prices/stock_prices.py index 9de20bc94..a0923a5b5 100644 --- a/stock_prices/stock_prices.py +++ b/stock_prices/stock_prices.py @@ -3,8 +3,31 @@ import argparse def find_max_profit(prices): - pass + # make a variable to hold current max profit + max_profit = -2 ** 100 + # set cursor at index 1 + # we want to sell after we bought + cur = 1 + + for i in range(len(prices)): + cur = i + 1 + + while cur < len(prices): + profit = prices[cur] - prices[i] + + if profit > max_profit: + max_profit = profit + + cur += 1 + + return max_profit + + + +print("Finding max profit") +prices = [1050, 270, 1540, 3800, 2] +print(find_max_profit(prices)) if __name__ == '__main__': # This is just some code to accept inputs from the command line From 34c6af2aa67f5bebcd1c91e0cd6185406c4bca1b Mon Sep 17 00:00:00 2001 From: Elizabeth Ter Sahakyan Date: Thu, 19 Mar 2020 14:00:19 -0700 Subject: [PATCH 2/3] recipe batches test passed --- recipe_batches/recipe_batches.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/recipe_batches/recipe_batches.py b/recipe_batches/recipe_batches.py index c845950c5..a4cd7b958 100644 --- a/recipe_batches/recipe_batches.py +++ b/recipe_batches/recipe_batches.py @@ -3,7 +3,29 @@ import math def recipe_batches(recipe, ingredients): - pass + + max_batches = 2 ** 100 + + batches = {} + for i in recipe: + # print(i) + try: + batches[i] = ingredients[i] // recipe[i] + except: + print(f'{i} not in ingredients') + return 0 + + for i in batches.values(): + if i < max_batches: + max_batches = i + + return max_batches + +recipe = { 'milk': 100, 'butter': 50, 'flour': 5 } +ingredients = { 'milk': 132, 'butter': 48, 'flour': 51 } +print(recipe_batches(recipe, ingredients)) + + if __name__ == '__main__': From 3e72feca08e659c5d36896b21c6dfd41d3b3a20b Mon Sep 17 00:00:00 2001 From: Elizabeth Ter Sahakyan Date: Thu, 19 Mar 2020 17:57:29 -0700 Subject: [PATCH 3/3] cookie test passed --- eating_cookies/eating_cookies.py | 108 ++++++++++++++++++++++++++++--- 1 file changed, 100 insertions(+), 8 deletions(-) diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 62655d803..c8ad9bbc1 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -6,11 +6,103 @@ # a solution that is more efficient than the naive # recursive solution def eating_cookies(n, cache=None): - pass - -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 + combinations = 0 + + if n == 0: + return 1 + + def fact(num): + if num < 1: + factorial = 0 + else: + factorial = 1 + while num >= 1: + factorial *= num + num -= 1 + return factorial + + # print('factorial check:', fact(3)) + + nums = [1, 2, 3] + + + # how many ways can you make combos with one number + for i in nums: + # print(f'starting {i}') + if (n / i) >= 1 and n % i == 0: + # print(f'1 number: {i}') + combinations += 1 + + print('combinations for 1:', combinations) + + # how many ways can you make combos with 2 numbers + two_num_combos = [] + for i in nums: + for j in nums: + if i != j: + if i + j <= n: + if [j,i] not in two_num_combos: + two_num_combos.append([i,j]) + + print('two num combos',two_num_combos) + print('solving equation for 2 num') + + for i in two_num_combos: + # xi[0] + yi[1] = n + # solve the equation + y = 1 + x_range = n // i[0] + 1 + y_range = n // i[1] + 1 + + print('x range', x_range) + print('y range', y_range) + + + for x in range(1, x_range): + for y in range(1, y_range): + if x*i[0] + y*i[1] == n: + print(x * [i[0]], y * [i[1]]) + print(f'x:{x}, y:{y}') + combinations += fact(x+y)// (fact(x)*fact(y)) + + print('fact',fact(x+y)//2) + + + # how many ways can you make combos with 3 numbers + three_num_combos = [] + + if sum(nums) <= n: + three_num_combos.append(nums) + + for i in three_num_combos: + # xi[0] + yi[1] + zi[2] = n + # solve the equation + + x_range = n // i[0] + 1 + y_range = n // i[1] + 1 + z_range = n // i[2] + 1 + + print('x range', x_range) + print('y range', y_range) + print('z range', z_range) + + + for x in range(1, x_range): + for y in range(1, y_range): + for z in range(1, z_range): + if x*i[0] + y*i[1] + z*i[2]== n: + print(x * [i[0]], y * [i[1]], z * [i[2]]) + print(f'x:{x}, y:{y}, z:{z}') + combinations += fact(x+y+z)// (fact(x)*fact(y)*fact(z)) + + + return combinations + +print(eating_cookies(0)) + +# 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