Skip to content

Commit d7fecbe

Browse files
added coordinate_compression algorithm
1 parent 43c3f4e commit d7fecbe

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

other/coordinate_compression.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""
2+
Assumption:
3+
- The values to compress are assumed to be comparable,
4+
- values can be sorted and compared with '<' and '>' operators.
5+
"""
6+
7+
8+
class CoordinateCompressor:
9+
"""
10+
A class for coordinate compression.
11+
12+
This class allows you to compress and decompress a list of values.
13+
14+
Mapping:
15+
In addition to compression and decompression, this class maintains a mapping
16+
between original values and their compressed counterparts using two data
17+
structures: a dictionary `coordinate_map` and a list `reverse_map`.
18+
19+
- `coordinate_map`: A dictionary that maps original values to their compressed
20+
coordinates. Keys are original values, and values are compressed coordinates.
21+
22+
- `reverse_map`: A list used for reverse mapping, where each index corresponds
23+
to a compressed coordinate, and the value at that index is the original value.
24+
25+
Example mapping:
26+
27+
Original: 10, Compressed: 0
28+
Original: 52, Compressed: 1
29+
Original: 83, Compressed: 2
30+
Original: 100, Compressed: 3
31+
32+
This mapping allows for efficient compression and decompression of values within
33+
the list.
34+
"""
35+
36+
def __init__(self, arr):
37+
"""
38+
Initialize the CoordinateCompressor with a list.
39+
40+
Args:
41+
arr (list): The list of values to be compressed.
42+
43+
>>> arr = [100, 10, 52, 83]
44+
>>> cc = CoordinateCompressor(arr)
45+
>>> cc.compress(100)
46+
3
47+
>>> cc.compress(52)
48+
1
49+
>>> cc.decompress(1)
50+
52
51+
52+
"""
53+
54+
self.coordinate_map = {} # A dictionary to store compressed coordinates
55+
self.reverse_map = [-1] * (len(arr)) # A list to store reverse mapping
56+
self.arr = sorted(arr) # The input list
57+
self.n = len(arr) # The length of the input list
58+
self.compress_coordinates()
59+
60+
def compress_coordinates(self):
61+
"""
62+
Compress the coordinates in the input list.
63+
"""
64+
key = 0
65+
for val in self.arr:
66+
if val not in self.coordinate_map:
67+
self.coordinate_map[val] = key
68+
self.reverse_map[key] = val
69+
key += 1
70+
71+
def compress(self, num):
72+
"""
73+
Compress a single value.
74+
75+
Args:
76+
num (any) : The value to compress.
77+
78+
Returns:
79+
int: The compressed integer, or -1 if not found in the original list.
80+
"""
81+
return self.coordinate_map.get(num, -1)
82+
83+
def decompress(self, num):
84+
"""
85+
Decompress a single integer.
86+
87+
Args:
88+
num (int): The compressed integer to decompress.
89+
90+
Returns:
91+
original value (any) : The original value.
92+
"""
93+
return self.reverse_map[num]
94+
95+
@staticmethod
96+
def how_to_use():
97+
"""
98+
Example usage of CoordinateCompressor.
99+
"""
100+
arr = [100, 10, 52, 83]
101+
cc = CoordinateCompressor(arr)
102+
compressed = [0] * len(arr)
103+
decompressed = [0] * len(arr)
104+
105+
for i in range(len(arr)):
106+
compressed[i] = cc.compress(arr[i])
107+
decompressed[i] = cc.decompress(compressed[i])
108+
print(f"Original: {arr[i]}, Compressed: {compressed[i]}")
109+
110+
111+
if __name__ == "__main__":
112+
from doctest import testmod
113+
114+
testmod()

0 commit comments

Comments
 (0)