From 7795a82757ec1352607c6da0fe0fa63d904c9971 Mon Sep 17 00:00:00 2001 From: Baisal89 Date: Mon, 23 Mar 2020 00:54:58 -0400 Subject: [PATCH 1/4] assigment --- eating_cookies/eating_cookies.py | 42 +++++++++++++++++++++++++++++--- eating_cookies/solving_prob.py | 13 ++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 eating_cookies/solving_prob.py diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 62655d803..c3d93b4e3 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -1,16 +1,52 @@ #!/usr/bin/python +#Fibinacci siquence +''' +# [0, 1, 1, 2, 3, 5, 8, 13, 21] +#defining recursive fibinacci +def rec_fib(n): # Global frame, function rec_fib + #base case + if n == 0: + return 1 + if n == 1: + return 1 + + return rec_fib(n-1) + rec_fib(n-2) + +print(rec_fib(6)) +''' 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 + #base case + if n == 0: + return 1 + elif n == 1: + return 1 + elif n == 2: + return 2 + elif n == 3: + return 4 + elif cache and cache[n]: + return cahe[n] + else: + cache[n] = eating_cookies(n-1, cache) + eating_cookies(n-2, cache) + eating_cookies(n-3, cache) + + + return cache[n] + + + return eating_cookies(n-1) + eating_cookies(n-2) + eating_cookies(n-3) 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 + print('Usage: eating_cookies.py [num_cookies]') + + +print(eating_cookies(4, {})) diff --git a/eating_cookies/solving_prob.py b/eating_cookies/solving_prob.py new file mode 100644 index 000000000..e43f626e1 --- /dev/null +++ b/eating_cookies/solving_prob.py @@ -0,0 +1,13 @@ +# print that factorial of # n + +def rec_factoria(n): + print(n) + #base case + if n <= 1: + return 1 + + #what step can we do recursively? + prev = rec_factoria(n-1) + return n * prev + +print(rec_factoria(999)) From 6d620c6f08815dc0080639f2f5f92f6fcba68ff7 Mon Sep 17 00:00:00 2001 From: Baisal89 Date: Mon, 23 Mar 2020 15:56:19 -0400 Subject: [PATCH 2/4] adding a file --- array.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 array.py diff --git a/array.py b/array.py new file mode 100644 index 000000000..e69de29bb From 7f7f1917f07479efbe05ca5d94d62bb8855d1817 Mon Sep 17 00:00:00 2001 From: Baisal89 Date: Thu, 16 Apr 2020 13:32:24 -0400 Subject: [PATCH 3/4] assignment 1 --- eating_cookies/eating_cookies.py | 2 +- problem1.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 problem1.py diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index c3d93b4e3..e9a85f9c2 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -31,7 +31,7 @@ def eating_cookies(n, cache=None): elif n == 3: return 4 elif cache and cache[n]: - return cahe[n] + return cache[n] else: cache[n] = eating_cookies(n-1, cache) + eating_cookies(n-2, cache) + eating_cookies(n-3, cache) diff --git a/problem1.py b/problem1.py new file mode 100644 index 000000000..ec449cdef --- /dev/null +++ b/problem1.py @@ -0,0 +1,30 @@ +# def binary_search(arr, target): +# # Set boundaries for low and high marks to search +# lo = 0 +# hi = len(arr) +# # While low and high do not overlap... +# while lo < hi: +# # Check the midpoint +# mid = (lo + hi) // 2 +# # If it's equal, return True +# if arr[mid] == target: +# return True +# # Else, if target is smaller +# elif target < arr[mid]: +# # set the high to midpoint value +# hi = mid +# # Else if target is bigger +# else: +# # set the low to midpoint value +# lo = mid + 1 +# # If we get to the end, return False +# return False + +# TO-DO: Complete the selection_sort() function below +def selection_sort( arr ): + # loop through n-1 elements + for i in range(0, len(arr) - 1): + cur_index = i + smallest_index = cur_index + # TO-DO: find next smallest element + # (hint, can do in 3 loc) \ No newline at end of file From 94e655df9a9849a2ec903ce45ef2ce4d416fb537 Mon Sep 17 00:00:00 2001 From: Baisal89 Date: Thu, 16 Apr 2020 20:47:03 -0400 Subject: [PATCH 4/4] assignment part 2 --- eating_cookies/eating_cookies.py | 54 +++++++++++++++++--------------- recipe_batches/recipe_batches.py | 18 ++++++++++- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index e9a85f9c2..3f5634448 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -21,32 +21,34 @@ def rec_fib(n): # Global frame, function rec_fib # a solution that is more efficient than the naive # recursive solution def eating_cookies(n, cache=None): - #base case - if n == 0: - return 1 - elif n == 1: - return 1 - elif n == 2: - return 2 - elif n == 3: - return 4 - elif cache and cache[n]: - return cache[n] - else: - cache[n] = eating_cookies(n-1, cache) + eating_cookies(n-2, cache) + eating_cookies(n-3, cache) - - - return cache[n] - - - return eating_cookies(n-1) + eating_cookies(n-2) + eating_cookies(n-3) - -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]') + #Check if cache is None. if so, initialize it to eppty dictionary + if cache is None: + cache = {} + #base case + if n == 0: + return 1 + elif n == 1: + return 1 + elif n == 2: + return 2 + elif n == 3: + return 4 + elif cache and cache[n]: + return cache[n] + else: + cache[n] = eating_cookies(n-1, cache) + eating_cookies(n-2, cache) + eating_cookies(n-3, cache) + + + return cache[n] + + + +# 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]') print(eating_cookies(4, {})) diff --git a/recipe_batches/recipe_batches.py b/recipe_batches/recipe_batches.py index c845950c5..a572e65bd 100644 --- a/recipe_batches/recipe_batches.py +++ b/recipe_batches/recipe_batches.py @@ -3,7 +3,23 @@ import math def recipe_batches(recipe, ingredients): - pass + count = 0 + first_val = list(ingredients.values())[0] // list(recipe.values())[0] + current_min = [] + for ing in recipe: + if ing not in ingredients or ingredients[ing] < recipe[ing]: + return 0 + val = ingredients[ing] // recipe[ing] + + if count == 0: + current_min.append(val) + elif val < current_min[len(current_min) - 1]: + current_min.append(val) + + count += 1 + + batch_size = current_min[len(current_min) - 1] + return batch_size if __name__ == '__main__':