-
Notifications
You must be signed in to change notification settings - Fork 311
/
ch-1.py
33 lines (24 loc) · 833 Bytes
/
ch-1.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
""" Challenge 267 Task 1 LK Python"""
from typing import List
def product_sign(ints: List[int]) -> int:
"""
Computes the sign of the product of a list of integers.
Args:
ints (List[int]): A list of integers.
Returns:
int: The sign of the product of the integers (1, -1, or 0).
"""
negative_count = 0
for value in ints:
if value == 0:
return 0
elif value < 0:
negative_count += 1
return -1 if negative_count % 2 != 0 else 1
# Test cases
assert product_sign([-1, -2, -3, -4, 3, 2, 1]) == 1
assert product_sign([1, 2, 0, -2, -1]) == 0
assert product_sign([-1, -1, 1, -1, 2]) == -1
print(product_sign([-1, -2, -3, -4, 3, 2, 1])) # Output: 1
print(product_sign([1, 2, 0, -2, -1])) # Output: 0
print(product_sign([-1, -1, 1, -1, 2])) # Output: -1