|
| 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 | + - `coordinate_map`: A dictionary that maps original values to their compressed |
| 19 | + coordinates. Keys are original values, and values are compressed coordinates. |
| 20 | + - `reverse_map`: A list used for reverse mapping, where each index corresponds |
| 21 | + to a compressed coordinate, and the value at that index is the original value. |
| 22 | +
|
| 23 | + Example of mapping: |
| 24 | + Original: 10, Compressed: 0 |
| 25 | + Original: 52, Compressed: 1 |
| 26 | + Original: 83, Compressed: 2 |
| 27 | + Original: 100, Compressed: 3 |
| 28 | +
|
| 29 | + This mapping allows for efficient compression and decompression of values within |
| 30 | + the list. |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__(self, arr: list[int | float | str]) -> None: |
| 34 | + """ |
| 35 | + Initialize the CoordinateCompressor with a list. |
| 36 | +
|
| 37 | + Args: |
| 38 | + arr: The list of values to be compressed. |
| 39 | +
|
| 40 | + >>> arr = [100, 10, 52, 83] |
| 41 | + >>> cc = CoordinateCompressor(arr) |
| 42 | + >>> cc.compress(100) |
| 43 | + 3 |
| 44 | + >>> cc.compress(52) |
| 45 | + 1 |
| 46 | + >>> cc.decompress(1) |
| 47 | + 52 |
| 48 | + """ |
| 49 | + |
| 50 | + # A dictionary to store compressed coordinates |
| 51 | + self.coordinate_map: dict[int | float | str, int] = {} |
| 52 | + |
| 53 | + # A list to store reverse mapping |
| 54 | + self.reverse_map: list[int | float | str] = [-1] * len(arr) |
| 55 | + |
| 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) -> None: |
| 61 | + """ |
| 62 | + 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[80] # Value not in the original list |
| 69 | + Traceback (most recent call last): |
| 70 | + ... |
| 71 | + KeyError: 80 |
| 72 | + >>> cc.reverse_map[2] |
| 73 | + 83 |
| 74 | + """ |
| 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 |
| 81 | + |
| 82 | + def compress(self, original: float | str) -> int: |
| 83 | + """ |
| 84 | + Compress a single value. |
| 85 | +
|
| 86 | + Args: |
| 87 | + original: The value to compress. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + The compressed integer, or -1 if not found in the original list. |
| 91 | +
|
| 92 | + >>> arr = [100, 10, 52, 83] |
| 93 | + >>> cc = CoordinateCompressor(arr) |
| 94 | + >>> cc.compress(100) |
| 95 | + 3 |
| 96 | + >>> cc.compress(7) # Value not in the original list |
| 97 | + -1 |
| 98 | + """ |
| 99 | + return self.coordinate_map.get(original, -1) |
| 100 | + |
| 101 | + def decompress(self, num: int) -> int | float | str: |
| 102 | + """ |
| 103 | + Decompress a single integer. |
| 104 | +
|
| 105 | + Args: |
| 106 | + num: The compressed integer to decompress. |
| 107 | +
|
| 108 | + Returns: |
| 109 | + The original value. |
| 110 | +
|
| 111 | + >>> arr = [100, 10, 52, 83] |
| 112 | + >>> cc = CoordinateCompressor(arr) |
| 113 | + >>> cc.decompress(0) |
| 114 | + 10 |
| 115 | + >>> cc.decompress(5) # Compressed coordinate out of range |
| 116 | + -1 |
| 117 | + """ |
| 118 | + return self.reverse_map[num] if 0 <= num < len(self.reverse_map) else -1 |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + from doctest import testmod |
| 123 | + |
| 124 | + testmod() |
| 125 | + |
| 126 | + arr: list[int | float | str] = [100, 10, 52, 83] |
| 127 | + cc = CoordinateCompressor(arr) |
| 128 | + |
| 129 | + for original in arr: |
| 130 | + compressed = cc.compress(original) |
| 131 | + decompressed = cc.decompress(compressed) |
| 132 | + print(f"Original: {decompressed}, Compressed: {compressed}") |
0 commit comments