Skip to content

Commit 399d6d7

Browse files
committed
Use built-in list type hints for Python 3.9+
1 parent 54f306d commit 399d6d7

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

data_structures/heap/median_in_a_stream.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import heapq
2-
from typing import List
32

43

54
def signum(a: int, b: int) -> int:
@@ -20,16 +19,15 @@ def signum(a: int, b: int) -> int:
2019

2120
def call_median(
2221
element: int,
23-
max_heap: List[int],
24-
min_heap: List[int],
22+
max_heap: list[int],
23+
min_heap: list[int],
2524
median: int,
2625
) -> int:
2726
"""
28-
Insert an element into heaps and update median.
27+
Insert an element into heaps and update the median.
2928
"""
3029
case = signum(len(max_heap), len(min_heap))
3130

32-
# Case 0: both heaps have same size
3331
if case == 0:
3432
if element > median:
3533
heapq.heappush(min_heap, element)
@@ -38,7 +36,6 @@ def call_median(
3836
heapq.heappush(max_heap, -element)
3937
median = -max_heap[0]
4038

41-
# Case 1: max heap has more elements
4239
elif case == 1:
4340
if element > median:
4441
heapq.heappush(min_heap, element)
@@ -47,7 +44,6 @@ def call_median(
4744
heapq.heappush(max_heap, -element)
4845
median = (-max_heap[0] + min_heap[0]) // 2
4946

50-
# Case -1: min heap has more elements
5147
else:
5248
if element > median:
5349
heapq.heappush(max_heap, -heapq.heappop(min_heap))
@@ -59,7 +55,7 @@ def call_median(
5955
return median
6056

6157

62-
def median_in_a_stream(numbers: List[int]) -> List[int]:
58+
def median_in_a_stream(numbers: list[int]) -> list[int]:
6359
"""
6460
Find the median after each insertion in a stream of integers.
6561
@@ -89,10 +85,10 @@ def median_in_a_stream(numbers: List[int]) -> List[int]:
8985
if not numbers:
9086
raise ValueError("Input list must not be empty")
9187

92-
max_heap: List[int] = []
93-
min_heap: List[int] = []
88+
max_heap: list[int] = []
89+
min_heap: list[int] = []
9490
median = 0
95-
result: List[int] = []
91+
result: list[int] = []
9692

9793
for element in numbers:
9894
median = call_median(element, max_heap, min_heap, median)

0 commit comments

Comments
 (0)