11"""
22Coordinate Compression Algorithm
33
4- Coordinate compression is used to reduce the range of numeric values
5- while preserving their order relationships. It’s often used in problems
6- like coordinate mapping or ranking .
4+ Coordinate compression reduces the range of numeric values while
5+ preserving their relative order. Useful in ranking, coordinate
6+ mapping, or segment trees .
77
88Example:
99 >>> compressor = CoordinateCompressor([100, 200, 300])
2626
2727class CoordinateCompressor :
2828 """
29- A class that performs coordinate compression and decompression.
29+ Class for coordinate compression and decompression.
3030
3131 Attributes:
32- values (List[int]): The sorted list of unique input values.
32+ values (List[int]): Sorted list of unique input values.
3333 """
3434
3535 def __init__ (self , values : List [int ]) -> None :
3636 """
3737 Initialize the compressor with a list of values.
3838
3939 Args:
40- values: A list of numeric values .
40+ values: List of integers to compress .
4141
4242 Raises:
43- ValueError: If the list is empty.
43+ ValueError: If input list is empty.
4444
4545 >>> CoordinateCompressor([5, 3, 8, 3]).values
4646 [3, 5, 8]
@@ -51,16 +51,16 @@ def __init__(self, values: List[int]) -> None:
5151
5252 def compress (self , value : int ) -> int :
5353 """
54- Compress a value to its index in the sorted list.
54+ Compress a value to its index in the sorted unique list.
5555
5656 Args:
57- value: The value to compress.
57+ value: Value to compress.
5858
5959 Returns:
60- The index of the value in the sorted list.
60+ Index of value in the sorted list.
6161
6262 Raises:
63- ValueError: If the value does not exist in the list .
63+ ValueError: If value is not present .
6464
6565 >>> comp = CoordinateCompressor([10, 20, 30])
6666 >>> comp.compress(20)
@@ -80,13 +80,13 @@ def decompress(self, index: int) -> int:
8080 Decompress an index back to its original value.
8181
8282 Args:
83- index: The compressed index.
83+ index: Compressed index.
8484
8585 Returns:
86- The original value corresponding to the index .
86+ Original value.
8787
8888 Raises:
89- ValueError: If the index is out of range .
89+ ValueError: If index is out of bounds .
9090
9191 >>> comp = CoordinateCompressor([1, 2, 3])
9292 >>> comp.decompress(0)
0 commit comments