Skip to content

Commit 08ce404

Browse files
adding return type hints and utilized enumerate
1 parent 767e4a5 commit 08ce404

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

other/coordinate_compression.py

Lines changed: 20 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):
36+
def __init__(self, arr: list) -> None:
3737
"""
3838
Initialize the CoordinateCompressor with a list.
3939
@@ -57,9 +57,19 @@ def __init__(self, arr):
5757
self.n = len(arr) # The length of the input list
5858
self.compress_coordinates()
5959

60-
def compress_coordinates(self):
60+
def compress_coordinates(self) -> None:
6161
"""
6262
Compress the coordinates in the input list.
63+
64+
>>> arr = [100, 10, 52, 83]
65+
>>> cc = CoordinateCompressor(arr)
66+
>>> cc.coordinate_map[83]
67+
2
68+
>>> cc.coordinate_map.get(80,-1) # Value not in the original list
69+
-1
70+
>>> cc.reverse_map[2] # Value not in the original list
71+
83
72+
6373
"""
6474
key = 0
6575
for val in self.arr:
@@ -68,12 +78,12 @@ def compress_coordinates(self):
6878
self.reverse_map[key] = val
6979
key += 1
7080

71-
def compress(self, num):
81+
def compress(self, original: any) -> int:
7282
"""
7383
Compress a single value.
7484
7585
Args:
76-
num (any) : The value to compress.
86+
original (any) : The value to compress.
7787
7888
Returns:
7989
int: The compressed integer, or -1 if not found in the original list.
@@ -86,9 +96,9 @@ def compress(self, num):
8696
-1
8797
8898
"""
89-
return self.coordinate_map.get(num, -1)
99+
return self.coordinate_map.get(original, -1)
90100

91-
def decompress(self, num):
101+
def decompress(self, num: int) -> any:
92102
"""
93103
Decompress a single integer.
94104
@@ -109,7 +119,7 @@ def decompress(self, num):
109119
return self.reverse_map[num] if num < len(self.reverse_map) else -1
110120

111121
@staticmethod
112-
def how_to_use():
122+
def how_to_use() -> None:
113123
"""
114124
Example usage of CoordinateCompressor.
115125
"""
@@ -118,10 +128,10 @@ def how_to_use():
118128
compressed = [0] * len(arr)
119129
decompressed = [0] * len(arr)
120130

121-
for i in range(len(arr)):
122-
compressed[i] = cc.compress(arr[i])
131+
for i, original in enumerate(arr):
132+
compressed[i] = cc.compress(original)
123133
decompressed[i] = cc.decompress(compressed[i])
124-
print(f"Original: {arr[i]}, Compressed: {compressed[i]}")
134+
print(f"Original: {original}, Compressed: {compressed[i]}")
125135

126136

127137
if __name__ == "__main__":

0 commit comments

Comments
 (0)