Skip to content

Robin Familara - cs-module-project-algorithms #103

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 5 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
12 changes: 10 additions & 2 deletions eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
Input: an integer
Returns: an integer
'''

def eating_cookies(n):
# Your code here
if n == 0 or n == 1:
return 1

if n == 2:
return 2

if n == 3:
return 4

pass
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
6 changes: 5 additions & 1 deletion moving_zeroes/moving_zeroes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
'''
def moving_zeroes(arr):
# Your code here
altered = [number for number in arr if number != 0]
zero = [number for number in arr if number is 0]
altered.extend(zero)
return altered

pass
print(moving_zeroes([0, 3, 1, 0, -2]))


if __name__ == '__main__':
Expand Down
12 changes: 11 additions & 1 deletion product_of_all_other_numbers/product_of_all_other_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
def product_of_all_other_numbers(arr):
# Your code here

pass
results = []

for index, number in enumerate(arr):
product = 1
excluding_current = arr[:index] + arr[index + 1:]
for number in excluding_current:
product *= number
results.append(product)
return results

print(product_of_all_other_numbers([1, 7, 3, 4]))


if __name__ == '__main__':
Expand Down
4 changes: 3 additions & 1 deletion single_number/single_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
def single_number(arr):
# Your code here

pass
for number in arr:
if arr.count(number) == 1:
return number


if __name__ == '__main__':
Expand Down
21 changes: 20 additions & 1 deletion sliding_window_max/sliding_window_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@
'''
def sliding_window_max(nums, k):
# Your code here

m = max(nums[:k])
result = [m]

pass
for i in range(1, len(nums)-k+1):
if nums[i+k-1] > m:
m = nums[i+k-1]
elif nums[i-1] == m:
m = max(nums[i:i+k])
result.append(m)
return result




if __name__ == '__main__':
# Use the main function here to test out your implementation
arr = [1, 3, -1, -3, 5, 3, 6, 7]
k = 3

print(f"Output of sliding_window_max function is: {sliding_window_max(arr, k)}")


if __name__ == '__main__':
Expand Down