Skip to content

Commit 9de0915

Browse files
committed
product except self today
1 parent 0e9eb3c commit 9de0915

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/product_except_self.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def productExceptSelf(self, nums: List[int]) -> List[int]:
6+
res = [1] * len(nums)
7+
8+
prefix = 1
9+
for i in range(len(nums)):
10+
res[i] = prefix
11+
prefix *= nums[i]
12+
13+
postfix = 1
14+
for i in range(len(nums) - 1, -1, -1):
15+
res[i] *= postfix
16+
postfix *= nums[i]
17+
return res

tests/test_product_except_self.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from product_except_self import Solution
2+
3+
testcases = [{"testcase": [1, 2, 3, 4], "result": [24, 12, 8, 6]}]
4+
5+
6+
def test_product_except_self():
7+
for tc in testcases:
8+
nums = tc["testcase"]
9+
ans = Solution().productExceptSelf(nums)
10+
assert ans == tc["result"]

0 commit comments

Comments
 (0)