Skip to content

Commit 767e4a5

Browse files
added coordinate_compression & doctest
1 parent d7fecbe commit 767e4a5

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

other/coordinate_compression.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ def compress(self, num):
7777
7878
Returns:
7979
int: The compressed integer, or -1 if not found in the original list.
80+
81+
>>> arr = [100, 10, 52, 83]
82+
>>> cc = CoordinateCompressor(arr)
83+
>>> cc.compress(100)
84+
3
85+
>>> cc.compress(7) # Value not in the original list
86+
-1
87+
8088
"""
8189
return self.coordinate_map.get(num, -1)
8290

@@ -89,8 +97,16 @@ def decompress(self, num):
8997
9098
Returns:
9199
original value (any) : The original value.
100+
101+
>>> arr = [100, 10, 52, 83]
102+
>>> cc = CoordinateCompressor(arr)
103+
>>> cc.decompress(0)
104+
10
105+
>>> cc.decompress(5) # Compressed coordinate out of range
106+
-1
107+
92108
"""
93-
return self.reverse_map[num]
109+
return self.reverse_map[num] if num < len(self.reverse_map) else -1
94110

95111
@staticmethod
96112
def how_to_use():

0 commit comments

Comments
 (0)