diff --git a/eating_cookies/eating_cookies.py b/eating_cookies/eating_cookies.py index aef505784..ed8b0e2bf 100644 --- a/eating_cookies/eating_cookies.py +++ b/eating_cookies/eating_cookies.py @@ -3,9 +3,15 @@ Returns: an integer ''' def eating_cookies(n): - # Your code here - pass + if n == 0 or n == 1: + return 1 + if n == 2: + return 2 + if n == 3: + return 4 + + return eating_cookies(n-1) + eating_cookies(n-2) + eating_cookies(n-3) 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..a1f62cc8e 100644 --- a/moving_zeroes/moving_zeroes.py +++ b/moving_zeroes/moving_zeroes.py @@ -3,13 +3,15 @@ Returns: a List of integers ''' def moving_zeroes(arr): - # Your code here - - pass + for num in arr: + if num == 0: + arr.remove(num) + arr.append(num) + return arr if __name__ == '__main__': # Use the main function here to test out your implementation arr = [0, 3, 1, 0, -2] - print(f"The resulting of moving_zeroes is: {moving_zeroes(arr)}") \ No newline at end of file + print(f"The resulting of moving_zeroes is: {moving_zeroes(arr)}") 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..e136d4f49 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,30 @@ Returns: a List of integers ''' def product_of_all_other_numbers(arr): - # Your code here + product_array = [] + index = 0 + + while index < len(arr): + + product = 1 + + for num in range(0, len(arr)): + if index == num: + continue + else: + product *= arr[num] + + product_array.append(product) + index += 1 + + return product_array + - 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] + # 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] - print(f"Output of product_of_all_other_numbers: {product_of_all_other_numbers(arr)}") + 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..b2ab45a11 100644 --- a/single_number/single_number.py +++ b/single_number/single_number.py @@ -3,13 +3,21 @@ Returns: an integer ''' def single_number(arr): - # Your code here + my_set = set() - pass + for num in arr: + if num not in my_set: + my_set.add(num) + else: + my_set.remove(num) + + list_from_set = list(my_set) + + return list_from_set[0] if __name__ == '__main__': # Use the main function to test your implementation arr = [1, 1, 4, 4, 5, 5, 3, 3, 9, 0, 0] - print(f"The odd-number-out is {single_number(arr)}") \ No newline at end of file + print(f"The odd-number-out is {single_number(arr)}") diff --git a/sliding_window_max/data/input.txt b/sliding_window_max/input.txt similarity index 100% rename from sliding_window_max/data/input.txt rename to sliding_window_max/input.txt diff --git a/sliding_window_max/data/output.txt b/sliding_window_max/output.txt similarity index 100% rename from sliding_window_max/data/output.txt rename to sliding_window_max/output.txt diff --git a/sliding_window_max/sliding_window_max.py b/sliding_window_max/sliding_window_max.py index 518ed36a2..af9190d7b 100644 --- a/sliding_window_max/sliding_window_max.py +++ b/sliding_window_max/sliding_window_max.py @@ -2,10 +2,25 @@ Input: a List of integers as well as an integer `k` representing the size of the sliding window Returns: a List of integers ''' -def sliding_window_max(nums, k): - # Your code here - pass +import collections +import itertools + +def sliding_window_max(nums, size): + arr = [] + + nums = iter(nums) + window = collections.deque( + itertools.islice(nums, size-1), + maxlen=size + ) + for item in nums: + window.append(item) + numbers = tuple(window) + max_num = max(numbers) + arr.append(max_num) + + return arr if __name__ == '__main__': diff --git a/sliding_window_max/test_sliding_window_max_large_input.py b/sliding_window_max/test_sliding_window_max_large_input.py index 840de6327..34b0e3ac2 100644 --- a/sliding_window_max/test_sliding_window_max_large_input.py +++ b/sliding_window_max/test_sliding_window_max_large_input.py @@ -7,13 +7,13 @@ def test_sliding_window_max_large_input(self): arr = [] k = 1000 - with open("data/input.txt") as file: + with open("input.txt") as file: for line in file: arr.append(int(line.strip())) expected = [] - with open("data/output.txt") as file: + with open("output.txt") as file: for line in file: expected.append(int(line.strip()))