-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.py
More file actions
31 lines (19 loc) · 818 Bytes
/
Copy path2.py
File metadata and controls
31 lines (19 loc) · 818 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
"""
This problem was asked by Uber.
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 math
numbers = [1, 2, 3, 4, 5]
def list_product_naive(nums: list[int]) -> list[int]:
products = []
product = math.prod(nums)
for num in nums:
products.append(product // num)
return products
print(list_product_naive(numbers))
"""
to solve without division, use a suffix and prefix pass to pre-calculate products stored in separate arrays
"""
# DONE