@@ -51,7 +51,7 @@ def __init__(self, arr: list[int | float | str]) -> None:
5151 self .coordinate_map : dict [int | float | str , int ] = {}
5252
5353 # A list to store reverse mapping
54- self .reverse_map : list [int | float | str ] = [- 1 ] * len ( arr )
54+ self .reverse_map : list [int | float | str ] = []
5555
5656 self .arr = sorted (arr ) # The input list
5757 self .n = len (arr ) # The length of the input list
@@ -72,12 +72,9 @@ def compress_coordinates(self) -> None:
7272 >>> cc.reverse_map[2]
7373 83
7474 """
75- key = 0
76- for val in self .arr :
77- if val not in self .coordinate_map :
78- self .coordinate_map [val ] = key
79- self .reverse_map [key ] = val
80- key += 1
75+ unique_arr = sorted (set (self .arr ))
76+ self .coordinate_map = {val : i for i , val in enumerate (unique_arr )}
77+ self .reverse_map = unique_arr
8178
8279 def compress (self , original : float | str ) -> int :
8380 """
@@ -87,16 +84,24 @@ def compress(self, original: float | str) -> int:
8784 original: The value to compress.
8885
8986 Returns:
90- The compressed integer, or -1 if not found in the original list.
87+ The compressed integer.
88+
89+ Raises:
90+ ValueError: If the value is not in the original list.
9191
9292 >>> arr = [100, 10, 52, 83]
9393 >>> cc = CoordinateCompressor(arr)
9494 >>> cc.compress(100)
9595 3
9696 >>> cc.compress(7) # Value not in the original list
97- -1
97+ Traceback (most recent call last):
98+ ...
99+ ValueError: 7 is not in the original list.
98100 """
99- return self .coordinate_map .get (original , - 1 )
101+ if original not in self .coordinate_map :
102+ msg = f"{ original } is not in the original list."
103+ raise ValueError (msg )
104+ return self .coordinate_map [original ]
100105
101106 def decompress (self , num : int ) -> int | float | str :
102107 """
@@ -108,14 +113,22 @@ def decompress(self, num: int) -> int | float | str:
108113 Returns:
109114 The original value.
110115
116+ Raises:
117+ ValueError: If the compressed coordinate is out of range.
118+
111119 >>> arr = [100, 10, 52, 83]
112120 >>> cc = CoordinateCompressor(arr)
113121 >>> cc.decompress(0)
114122 10
115123 >>> cc.decompress(5) # Compressed coordinate out of range
116- -1
124+ Traceback (most recent call last):
125+ ...
126+ ValueError: Compressed coordinate 5 is out of range.
117127 """
118- return self .reverse_map [num ] if 0 <= num < len (self .reverse_map ) else - 1
128+ if not (0 <= num < len (self .reverse_map )):
129+ msg = f"Compressed coordinate { num } is out of range."
130+ raise ValueError (msg )
131+ return self .reverse_map [num ]
119132
120133
121134if __name__ == "__main__" :
0 commit comments