diff --git a/array.py b/array.py new file mode 100644 index 000000000..e69de29bb diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index 62655d803..3f5634448 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -1,16 +1,54 @@ #!/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 - -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 + #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/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)) 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 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__':