-
Notifications
You must be signed in to change notification settings - Fork 892
/
problem_002.py
30 lines (25 loc) · 866 Bytes
/
problem_002.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def get_factors(array):
cumulative_product = 1
right_prod_array = list()
for num in array:
cumulative_product *= num
right_prod_array.append(cumulative_product)
cumulative_product = 1
left_prod_array = list()
for num in array[::-1]:
cumulative_product *= num
left_prod_array.append(cumulative_product)
left_prod_array = left_prod_array[::-1]
output_array = list()
for i in range(len(array)):
num = None
if i == 0:
num = left_prod_array[i + 1]
elif i == len(array) - 1:
num = right_prod_array[i - 1]
else:
num = right_prod_array[i - 1] * left_prod_array[i + 1]
output_array.append(num)
return output_array
assert get_factors([1, 2, 3, 4, 5]) == [120, 60, 40, 30, 24]
assert get_factors([3, 2, 1]) == [2, 3, 6]