diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index aef505784..ed84fa8da 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -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 diff --git a/moving_zeroes/moving_zeroes.py b/moving_zeroes/moving_zeroes.py index 2976ae48f..8751c7dfe 100644 --- a/moving_zeroes/moving_zeroes.py +++ b/moving_zeroes/moving_zeroes.py @@ -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__': diff --git a/product_of_all_other_numbers/product_of_all_other_numbers.py b/product_of_all_other_numbers/product_of_all_other_numbers.py index 7d976257a..11510ee3f 100644 --- a/product_of_all_other_numbers/product_of_all_other_numbers.py +++ b/product_of_all_other_numbers/product_of_all_other_numbers.py @@ -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)}") diff --git a/single_number/single_number.py b/single_number/single_number.py index be6fd6278..856c48fa2 100644 --- a/single_number/single_number.py +++ b/single_number/single_number.py @@ -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__': diff --git a/sliding_window_max/sliding_window_max.py b/sliding_window_max/sliding_window_max.py index 518ed36a2..daf9b6af8 100644 --- a/sliding_window_max/sliding_window_max.py +++ b/sliding_window_max/sliding_window_max.py @@ -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__':