Skip to content

Joel gonzalez #124

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 2 commits 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
15 changes: 13 additions & 2 deletions eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
Input: an integer
Returns: an integer
'''
def eating_cookies(n):
def eating_cookies(n, cache=None):
# Your code here

pass
if cache == None:
cache = [0 for _ in range(n + 1)]


if n == 0:
cache[n] = 1
if n == 1 or n == 2:
cache[n] = n
if cache[n] == 0:
cache[n] = eating_cookies(n - 1, cache) + eating_cookies(n - 2, cache) + eating_cookies(n - 3, cache)

return cache[n]

if __name__ == "__main__":
# Use the main function here to test out your implementation
Expand Down
27 changes: 26 additions & 1 deletion moving_zeroes/moving_zeroes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,32 @@
def moving_zeroes(arr):
# Your code here

pass
# set the left index to 0 and the right index to the last value in the array
left_index = 0
right_index = len(arr) - 1

# while the left index is not 0 and the left index is not the right index
while arr[left_index] != 0 and left_index != right_index:
#increment the left index by one
left_index += 1

# while the right index is equal to 0 and the right index is not the left index
while arr[right_index] == 0 and right_index != left_index:
# decrement the right index by 1
right_index -= 1

# while the left side is less than the right
while left_index < right_index:
# if the left index is 0 and the right is not 0
if arr[left_index] == 0 and arr[right_index] != 0:
# swap the position of the left and right indexes
arr[left_index], arr[right_index] = arr[right_index], arr[left_index]

#increment both indexes
left_index += 1
right_index -= 1
return arr



if __name__ == '__main__':
Expand Down
25 changes: 21 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,31 @@
Returns: a List of integers
'''
def product_of_all_other_numbers(arr):
# Your code here

# initialize an empty list to hold the result of the products
result = []

pass
# loop over the array and set the product index to 1
for i in range(len(arr)):
product = 1
# loop a second time and if its not the current index then multiply the product by the rest of the array
for j in range(len(arr)):
if i != j:
product *= arr[j]
# append the product to the result list
result.append(product)

return result







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)}")
32 changes: 31 additions & 1 deletion single_number/single_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,40 @@
Input: a List of integers where every int except one shows up twice
Returns: an integer
'''

def single_number(arr):
# Your code here
unique_num = set()


for num in arr:
if num in unique_num:
unique_num.remove(num)
else:
unique_num.add(num)


(unique, ) = unique_num

return unique


















pass


if __name__ == '__main__':
Expand Down
30 changes: 29 additions & 1 deletion sliding_window_max/sliding_window_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,38 @@
Input: a List of integers as well as an integer `k` representing the size of the sliding window
Returns: a List of integers
'''

from collections import deque

def sliding_window_max(nums, k):
# Your code here

pass
max_vals = []
q = deque()
# remove all elems from a queue
for i, n in enumerate(nums):
while len(q) > 0 and n > q[-1]:
q.pop()

q.append(n)

# calc the window range
window_range = i - k + 1

# as long as our windows range == k, then we will add elements to the queue
if window_range >= 0:
# add the max elem (in this case 1st in the queue) to the max_vals
max_vals.append(q[0])

# check num on the left needs to be evicted
# if so take it out of the start of the queue
if nums[window_range] == q[0]:
q.popleft()

return max_vals





if __name__ == '__main__':
Expand Down