Skip to content
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
19 changes: 16 additions & 3 deletions eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@
Input: an integer
Returns: an integer
'''
def eating_cookies(n):
# Your code here
def eating_cookies(n, cache = None):
# base case
if n < 0:
return 0

if n == 0:
return 1


elif cache is not None and cache[n] > 0:
return cache[n]
else:
if cache is None:
cache = [0 for i in range(n + 1)]
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
16 changes: 14 additions & 2 deletions moving_zeroes/moving_zeroes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
Returns: a List of integers
'''
def moving_zeroes(arr):
# Your code here
# The count of non-zero elements
count = 0

pass
# if the element is not a zero, then replace the element at index 'count' with this element
for i in range(len(arr)):
if(arr[i] != 0):
arr[count] = arr[i]
# increment count by 1
count += 1

# After all the non-zero elements have been shifted to the front of the list and 'count' is set as the index of first 0
# make all the elements 0 from 'count' to the end of the list
while count < len(arr):
arr[count] = 0
count += 1
return arr

if __name__ == '__main__':
# Use the main function here to test out your implementation
Expand Down
10 changes: 8 additions & 2 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,9 +3,15 @@
Returns: a List of integers
'''
def product_of_all_other_numbers(arr):
# Your code here
# Start with 1. If we start with 0 it will multiply by 0
result = 1

pass
# This while loop through our array, and multiply each value with the result
for x in arr:
# After the multiplication, we'll have a new result so keep multiplying to, until we run out of elements in our array
result *= x
# Our result (total) will then be divided by the number in our list.
return [result / x for x in arr]


if __name__ == '__main__':
Expand Down
11 changes: 8 additions & 3 deletions single_number/single_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
Returns: an integer
'''
def single_number(arr):
# Your code here

pass
set_1, set_2 = set(), set()

for n in arr:
if n not in set_1:
set_1.add(n)
else:
set_2.add(n)

return (set_1 - set_2).pop()

if __name__ == '__main__':
# Use the main function to test your implementation
arr = [1, 1, 4, 4, 5, 5, 3, 3, 9, 0, 0]
Expand Down
12 changes: 10 additions & 2 deletions sliding_window_max/sliding_window_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
Returns: a List of integers
'''
def sliding_window_max(nums, k):
# Your code here
max = 0
n = len(nums)
arr1 = []

pass
for i in range(n - k + 1):
max = nums[i]
for j in range(1, k):
if nums[i + j] > max:
max = nums[i + j]
arr1.append(max)
return arr1


if __name__ == '__main__':
Expand Down