-
Notifications
You must be signed in to change notification settings - Fork 0
/
Product of Array Except Self.py
54 lines (36 loc) · 1.38 KB
/
Product of Array Except Self.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def productExceptSelf(nums):
n = len(nums)
answer = [1] * n # Initialize the answer array with 1s
# Compute left products
left_product = 1
for i in range(n):
answer[i] = left_product
left_product *= nums[i]
# Compute right products and update the answer
right_product = 1
for i in range(n - 1, -1, -1):
answer[i] *= right_product
right_product *= nums[i]
return answer
# Test the function with the provided examples
example1 = [1, 2, 3, 4]
output1 = productExceptSelf(example1)
print(output1)
# more concise and modern version of the productExceptSelf function in Python by utilizing list comprehensions and the zip function.
def productExceptSelf(nums):
n = len(nums)
left_products = [1]
right_products = [1]
# Generate left products
[left_products.append(left_products[-1] * nums[i]) for i in range(n - 1)]
# Generate right products in reverse
[right_products.append(right_products[-1] * nums[-1 - i]) for i in range(n - 1)]
# Reverse right_products for easy pairing with left_products
right_products.reverse()
# Combine left and right products
answer = [l * r for l, r in zip(left_products, right_products)]
return answer
# Test the function
example1 = [1, 2, 3, 4]
output1 = productExceptSelf(example1)
print(output1)