Skip to content
This repository was archived by the owner on Jul 18, 2020. It is now read-only.
Open
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
Empty file added array.py
Empty file.
56 changes: 47 additions & 9 deletions eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
@@ -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]')
#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, {}))
13 changes: 13 additions & 0 deletions eating_cookies/solving_prob.py
Original file line number Diff line number Diff line change
@@ -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))
30 changes: 30 additions & 0 deletions problem1.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 17 additions & 1 deletion recipe_batches/recipe_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down