Skip to content

Commit 6a0061a

Browse files
added type hints, for list and dict
1 parent cf2194d commit 6a0061a

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

other/coordinate_compression.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CoordinateCompressor:
3333
the list.
3434
"""
3535

36-
def __init__(self, arr: list) -> None:
36+
def __init__(self, arr: list[int | float | str]) -> None:
3737
"""
3838
Initialize the CoordinateCompressor with a list.
3939
@@ -51,8 +51,14 @@ def __init__(self, arr: list) -> None:
5151
5252
"""
5353

54-
self.coordinate_map = {} # A dictionary to store compressed coordinates
55-
self.reverse_map = [-1] * (len(arr)) # A list to store reverse mapping
54+
self.coordinate_map: dict[
55+
int | float | str, int
56+
] = {} # A dictionary to store compressed coordinates
57+
58+
self.reverse_map: list[int | float | str] = [-1] * (
59+
len(arr)
60+
) # A list to store reverse mapping
61+
5662
self.arr = sorted(arr) # The input list
5763
self.n = len(arr) # The length of the input list
5864
self.compress_coordinates()
@@ -78,12 +84,12 @@ def compress_coordinates(self) -> None:
7884
self.reverse_map[key] = val
7985
key += 1
8086

81-
def compress(self, original: any) -> int:
87+
def compress(self, original: float | str) -> int:
8288
"""
8389
Compress a single value.
8490
8591
Args:
86-
original (any) : The value to compress.
92+
original (int | float | str) : The value to compress.
8793
8894
Returns:
8995
int: The compressed integer, or -1 if not found in the original list.
@@ -98,15 +104,15 @@ def compress(self, original: any) -> int:
98104
"""
99105
return self.coordinate_map.get(original, -1)
100106

101-
def decompress(self, num: int) -> any:
107+
def decompress(self, num: int) -> int | float | str:
102108
"""
103109
Decompress a single integer.
104110
105111
Args:
106112
num (int): The compressed integer to decompress.
107113
108114
Returns:
109-
original value (any) : The original value.
115+
original value (int | float | str) : The original value.
110116
111117
>>> arr = [100, 10, 52, 83]
112118
>>> cc = CoordinateCompressor(arr)
@@ -124,10 +130,10 @@ def decompress(self, num: int) -> any:
124130

125131
testmod()
126132

127-
arr = [100, 10, 52, 83]
133+
arr: list[int | float | str] = [100, 10, 52, 83]
128134
cc = CoordinateCompressor(arr)
129-
compressed = [0] * len(arr)
130-
decompressed = [0] * len(arr)
135+
compressed: list[int] = [0] * len(arr)
136+
decompressed: list[int | float | str] = [0] * len(arr)
131137

132138
for i, original in enumerate(arr):
133139
compressed[i] = cc.compress(original)

0 commit comments

Comments
 (0)