-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsolution.py
More file actions
49 lines (42 loc) · 1.13 KB
/
solution.py
File metadata and controls
49 lines (42 loc) · 1.13 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
41
42
43
44
45
46
47
48
49
SUBMIT = True
def min_max_list(_items: list[int]) -> tuple[int, int] | None:
"""Find both min and max values in a list by iterating once.
Example usage:
>>> min_max_list([3, 1, 4, 1, 5, 9, 2, 6, 5])
(1, 9)
>>> min_max_list([10])
(10, 10)
>>> min_max_list([])
None
"""
# El mayor entero que cabe en una "palabra" de memoria (64 bits)
a = float('inf')
b = float('-inf')
if not _items:
return None
for i in _items:
if i > b:
b =i
if i < a:
a=i
return (a,b)
def test() -> None:
"""Simple self-test for Min/Max List."""
cases = [
([3, 1, 4, 1, 5, 9, 2, 6, 5], (1, 9)),
([10], (10, 10)),
([], None),
([-1, -5, 0, 10], (-5, 10)),
]
for items, expected in cases:
try:
res = min_max_list(items)
assert res == expected, (
f"Failed for {items}: expected {expected}, got {res}"
)
except AssertionError as e:
print(f"❌ {e}")
return
print("✅ All tests passed!")
if __name__ == "__main__":
test()