-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduct_except.py
More file actions
40 lines (29 loc) · 1.15 KB
/
product_except.py
File metadata and controls
40 lines (29 loc) · 1.15 KB
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
39
40
"""
You have a list of integers, and for each index you want to find the product of
every integer except the integer at that index. Write a function
get_products_of_all_ints_except_at_index() that takes a list of integers and
returns a list of the products. (Interview Cake)
>>> [1, 7, 3, 4]
[84, 12, 28, 21]
>>> [0, 1, 2, 3]
[6, 0, 0, 0]
"""
def get_products_of_all_ints_except_at_index(nums):
if len(nums) < 2:
raise IndexError("Array must contain two or more integers.")
products_of_all_nums_except_at_index = []
product_so_far = 1
# First loop populates list with products of all numbers before
# corresponding index
for i in xrange(len(nums)):
products_of_all_nums_except_at_index.append(product_so_far)
product_so_far *= nums[i]
product_so_far = 1
j = -1
# Then, we loop again, multiplying each number in the list by the product of
# the numbers after its index, starting from the end of the list
for i in xrange(len(nums)):
products_of_all_nums_except_at_index[j] *= product_so_far
product_so_far *= nums[j]
j -= 1
return products_of_all_nums_except_at_index