Skip to content

Finished single_number, moving_zeroes, and sliding_window #104

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 7 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
10 changes: 8 additions & 2 deletions eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions moving_zeroes/moving_zeroes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")
print(f"The resulting of moving_zeroes is: {moving_zeroes(arr)}")
28 changes: 22 additions & 6 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,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)}")
14 changes: 11 additions & 3 deletions single_number/single_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")
print(f"The odd-number-out is {single_number(arr)}")
File renamed without changes.
File renamed without changes.
21 changes: 18 additions & 3 deletions sliding_window_max/sliding_window_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down
4 changes: 2 additions & 2 deletions sliding_window_max/test_sliding_window_max_large_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand Down