Skip to content

initial commit #116

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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Module Project - Algorithms
..............................
https://github.com/LambdaSchool/cs-module-project-algorithms/pull/116
..............................

## Sprint: Algorithms

Expand Down
15 changes: 12 additions & 3 deletions eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
Returns: an integer
'''
def eating_cookies(n):
# Your code here

pass
r = -1

if n < 0:
r = 0

elif n == 0:
r = 1

else:
r = eating_cookies(n-1) + eating_cookies(n-2) + eating_cookies(n-3)
return r


if __name__ == "__main__":
# Use the main function here to test out your implementation
Expand Down
33 changes: 20 additions & 13 deletions making_change/making_change.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
#!/usr/bin/python

import sys

def making_change(amount, denominations):
# Your code here

pass
def eating_cookies(n, cache={0: 1, 1: 1, 2: 2}):
"""
n - int - Number of cookies in the jar
cache - dict - memoization of cookie methods already calculated
"""

if n in cache:
return cache[n]
else:
cache[n] = eating_cookies(n - 3) + eating_cookies(n - 2) + eating_cookies(n - 1)
return cache[n]


if __name__ == "__main__":
# Test our your implementation from the command line
# with `python making_change.py [amount]` with different amounts
if len(sys.argv) > 1:
denominations = [1, 5, 10, 25, 50]
amount = int(sys.argv[1])
print("There are {ways} ways to make {amount} cents.".format(ways=making_change(amount, denominations), amount=amount))
else:
print("Usage: making_change.py [amount]")
if len(sys.argv) > 1:
num_cookies = int(sys.argv[1])
print(
"There are {ways} ways for Cookie Monster to eat {n} cookies.".format(
ways=eating_cookies(num_cookies), n=num_cookies
)
)
else:
print("Usage: eating_cookies.py [num_cookies]")
21 changes: 17 additions & 4 deletions moving_zeroes/moving_zeroes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@
Input: a List of integers
Returns: a List of integers
'''
def moving_zeroes(arr):
# Your code here

pass
# function which pushes all zeros to the end of an array
def moving_zeroes(arr):
count = 0
mock_arr = arr.copy()

if len(arr) == 0:
return arr

for i in mock_arr:
if i == 0:
arr.remove(i)
arr.append(i)
count += 1

return arr


# move all zeros present in the list to the end
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)}")
7 changes: 3 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 @@ -2,15 +2,14 @@
Input: a List of integers
Returns: a List of integers
'''
from functools import reduce
def product_of_all_other_numbers(arr):
# Your code here

pass
return [ reduce(lambda x, y: x * y, arr[:i] + arr[i+1:]) for i in range(len(arr))]


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]

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)}")
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def test_product_of_all_other_numbers_division_friendly(self):
self.assertEqual(product_of_all_other_numbers(arr), expected)

# Uncomment this test to test your solution that doesn't utilize division
# def test_product_of_all_other_numbers_without_division(self):
# arr = [7, 9, 1, 8, 6, 0, 7, 8, 8, 7, 10]
# expected = [0, 0, 0, 0, 0, 94832640, 0, 0, 0, 0, 0]
def test_product_of_all_other_numbers_without_division(self):
arr = [7, 9, 1, 8, 6, 0, 7, 8, 8, 7, 10]
expected = [0, 0, 0, 0, 0, 94832640, 0, 0, 0, 0, 0]

# self.assertEqual(product_of_all_other_numbers(arr), expected)
self.assertEqual(product_of_all_other_numbers(arr), expected)

if __name__ == '__main__':
unittest.main()
5 changes: 2 additions & 3 deletions single_number/single_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
Returns: an integer
'''
def single_number(arr):
# Your code here

pass
# applying the formula
return 2 * sum(set(arr)) - sum(arr)


if __name__ == '__main__':
Expand Down
19 changes: 13 additions & 6 deletions sliding_window_max/sliding_window_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
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

def sliding_window_max(arr, k):
max = 0
x = []
for i in range(len(arr) - k + 1):
max = arr[i]
for j in range(1, k):
if arr[i + j] > max:
max = arr[i + j]
x.append(max)

return x

# Driver method
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)}")
print(f"Output of sliding_window_max function is: {sliding_window_max(arr, k)}")