Skip to content

Finished MVP #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
44 changes: 41 additions & 3 deletions eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,48 @@
Input: an integer
Returns: an integer
'''
def eating_cookies(n):
# Your code here
# def eating_cookies(n):
# # 3 choices to eat cookies
# # there is only one way to eat one cookie
# if n < 0:
# return 0
# if n==0 :
# return 1
# else:
# return eating_cookies(n-1) + eating_cookies(n-2)+ eating_cookies(n-3)
# #there is a lot of redundant work in first solution
# use datastructure to cache(or save) the answer that has already calculated
# by other branches
#what datastructure should we use for the cache?
#save the n value and answer associated with the n value
#we need to pass the cache the dictionary to the recursive call
# to access as well as update the cache

#with cache every n value computed once
# the cache reduce the runtime from O( 3^n) to O(n)

def eating_cookies(n, cache):
# 3 choices to eat cookies
# there is only one way to eat one cookie
if n < 0:
return 0
if n==0 :
return 1
#check the cache to see if it holds the answer for the branch
elif n in cache:
return cache[n]

else:
#otherwise, n is not in the cache, so we need to calculate the anwser for the branch
#save the answer in the cache
cache[n] = eating_cookies(n-1, cache) + eating_cookies(n-2, cache)+ eating_cookies(n-3, cache)
return cache[n]






pass

if __name__ == "__main__":
# Use the main function here to test out your implementation
Expand Down
2 changes: 1 addition & 1 deletion eating_cookies/test_eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_eating_cookies_large_n(self):
self.assertEqual(eating_cookies(50, [0 for i in range(51)]), 10562230626642)
self.assertEqual(eating_cookies(100, [0 for i in range(101)]), 180396380815100901214157639)
self.assertEqual(eating_cookies(500, [0 for i in range(501)]), 1306186569702186634983475450062372018715120191391192207156664343051610913971927959744519676992404852130396504615663042713312314219527)

# pass

if __name__ == '__main__':
unittest.main()
18 changes: 15 additions & 3 deletions moving_zeroes/moving_zeroes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@
Returns: a List of integers
'''
def moving_zeroes(arr):
# Your code here
"""
list ->list
Return list of shifted non_zero element to the left
"""
j = 0
for i in range(1, len(arr)):
if arr[i] !=0:
# swap the value with zero element of the list
arr[j], arr[i] =arr[i], arr[j]
#move to the next zero element of the list
j = j+1

return arr

pass


if __name__ == '__main__':
# Use the main function here to test out your implementation
arr = [0, 3, 1, 0, -2]
#arr = [0, 3, 1, 0, -2]
arr = [0, 0, 0, 0, 3, 2, 1]

print(f"The resulting of moving_zeroes is: {moving_zeroes(arr)}")
17 changes: 13 additions & 4 deletions product_of_all_other_numbers/product_of_all_other_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
Returns: a List of integers
'''
def product_of_all_other_numbers(arr):
# Your code here
#loop through each element in the list
new_arr = []
for i in range(0, len(arr)):
#loop through every element
product = 1
for j in range(0, len(arr)):
if i != j:
product = product *arr[j]
new_arr.append(product)
return new_arr


pass


if __name__ == '__main__':
# Use the main function to test your implementation
# arr = [1, 2, 3, 4, 5]
arr = [2, 6, 9, 8, 2, 2, 9, 10, 7, 4, 7, 1, 9, 5, 9, 1, 8, 1, 8, 6, 2, 6, 4, 8, 9, 5, 4, 9, 10, 3, 9, 1, 9, 2, 6, 8, 5, 5, 4, 7, 7, 5, 8, 1, 6, 5, 1, 7, 7, 8]
arr = [1, 2, 3, 4, 5]
# arr = [2, 6, 9, 8, 2, 2, 9, 10, 7, 4, 7, 1, 9, 5, 9, 1, 8, 1, 8, 6, 2, 6, 4, 8, 9, 5, 4, 9, 10, 3, 9, 1, 9, 2, 6, 8, 5, 5, 4, 7, 7, 5, 8, 1, 6, 5, 1, 7, 7, 8]

print(f"Output of product_of_all_other_numbers: {product_of_all_other_numbers(arr)}")
64 changes: 61 additions & 3 deletions single_number/single_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,71 @@
Returns: an integer
'''
def single_number(arr):
# Your code here
'''
list ->int
Return odd-number-out element
'''
# # check every element appears once or not
for i in range(0, len(arr)):
j=i+1
#keep comparing i th element with the others element
# until we find match
count = ''
while count !='found' and j < len(arr)-1:
if arr[i] == arr[j]:
count = 'found'
#pop the value from the array
arr.pop(j)
else:
j=j+1
# Return the number if we did not find the match
if count !='found':
return arr[i]
'''
Better soluntion with singlr loop but still O(n^2)
count function has O(n) runtime
Better solution in reading perspective
Is there datastructure that would help us
Dictionary and setsis a good datastructure in finding the duplicate values
Space Complexity - addition datastructure or varibale used in the program to store that
'''

def single_number(arr):
'''
list ->int
Return odd-number-out element
'''
for x in arr:
if arr.count(x) ==1:
return x

def single_number(arr):
'''
list ->int
Return odd-number-out element
'''
# as far as the space complexity concerned
#worst case would be we have half of the element store
#in the sets before we start removing it
#initialize a sets-O(1)
s = set()
#loop through every element in the list
for i in arr: #O(n)
if i in s: #O(1)
s.remove(x) # O(1)
else:
s.add(x) # O(1)
return list(s)[0] # O(1)

pass







if __name__ == '__main__':
# Use the main function to test your implementation
arr = [1, 1, 4, 4, 5, 5, 3, 3, 9, 0, 0]
arr = [1, 1, 5, 3,5, 3, 9, 0, 0]

print(f"The odd-number-out is {single_number(arr)}")