-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem2.py
More file actions
38 lines (28 loc) · 969 Bytes
/
Copy pathproblem2.py
File metadata and controls
38 lines (28 loc) · 969 Bytes
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
31
32
33
34
35
36
37
38
# Given an array of integers, return a new array such that
# each element at index i of the new array is the product of all the
# numbers in the original array except the one at i.
# For example, if our input was [1, 2, 3, 4, 5], the expected output would be
# [120, 60, 40, 30, 24]. If our input was [3, 2, 1],
# the expected output would be [2, 3, 6].
# Follow-up: what if you can't use division?
import basics
#O(n) time, O(n) space
def compute(input_list):
n = len(input_list)
products = [1 for i in range(n)]
temp = 1
for i in range(n):
products[i] = temp
temp *= input_list[i]
temp = 1
for i in range(n-1, -1, -1):
products[i] *= temp
temp *= input_list[i]
return products
def main():
assert compute([3,2,1]) == [2,3,6]
assert compute([1, 2, 3, 4, 5]) == [120, 60, 40, 30, 24]
input_list = basics.getNumberList()
print(compute(input_list))
if __name__ == '__main__':
main()