From d7fecbef8449c54c5119cf6474dea98e5d4299ec Mon Sep 17 00:00:00 2001 From: Om Ashishkumar Soni Date: Sun, 1 Oct 2023 22:52:20 +0530 Subject: [PATCH 01/29] added coordinate_compression algorithm --- other/coordinate_compression.py | 114 ++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 other/coordinate_compression.py diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py new file mode 100644 index 000000000000..935901e32cff --- /dev/null +++ b/other/coordinate_compression.py @@ -0,0 +1,114 @@ +""" +Assumption: + - The values to compress are assumed to be comparable, + - values can be sorted and compared with '<' and '>' operators. +""" + + +class CoordinateCompressor: + """ + A class for coordinate compression. + + This class allows you to compress and decompress a list of values. + + Mapping: + In addition to compression and decompression, this class maintains a mapping + between original values and their compressed counterparts using two data + structures: a dictionary `coordinate_map` and a list `reverse_map`. + + - `coordinate_map`: A dictionary that maps original values to their compressed + coordinates. Keys are original values, and values are compressed coordinates. + + - `reverse_map`: A list used for reverse mapping, where each index corresponds + to a compressed coordinate, and the value at that index is the original value. + + Example mapping: + + Original: 10, Compressed: 0 + Original: 52, Compressed: 1 + Original: 83, Compressed: 2 + Original: 100, Compressed: 3 + + This mapping allows for efficient compression and decompression of values within + the list. + """ + + def __init__(self, arr): + """ + Initialize the CoordinateCompressor with a list. + + Args: + arr (list): The list of values to be compressed. + + >>> arr = [100, 10, 52, 83] + >>> cc = CoordinateCompressor(arr) + >>> cc.compress(100) + 3 + >>> cc.compress(52) + 1 + >>> cc.decompress(1) + 52 + + """ + + self.coordinate_map = {} # A dictionary to store compressed coordinates + self.reverse_map = [-1] * (len(arr)) # A list to store reverse mapping + self.arr = sorted(arr) # The input list + self.n = len(arr) # The length of the input list + self.compress_coordinates() + + def compress_coordinates(self): + """ + Compress the coordinates in the input list. + """ + key = 0 + for val in self.arr: + if val not in self.coordinate_map: + self.coordinate_map[val] = key + self.reverse_map[key] = val + key += 1 + + def compress(self, num): + """ + Compress a single value. + + Args: + num (any) : The value to compress. + + Returns: + int: The compressed integer, or -1 if not found in the original list. + """ + return self.coordinate_map.get(num, -1) + + def decompress(self, num): + """ + Decompress a single integer. + + Args: + num (int): The compressed integer to decompress. + + Returns: + original value (any) : The original value. + """ + return self.reverse_map[num] + + @staticmethod + def how_to_use(): + """ + Example usage of CoordinateCompressor. + """ + arr = [100, 10, 52, 83] + cc = CoordinateCompressor(arr) + compressed = [0] * len(arr) + decompressed = [0] * len(arr) + + for i in range(len(arr)): + compressed[i] = cc.compress(arr[i]) + decompressed[i] = cc.decompress(compressed[i]) + print(f"Original: {arr[i]}, Compressed: {compressed[i]}") + + +if __name__ == "__main__": + from doctest import testmod + + testmod() From 767e4a539b87409e4653f2167d05935cb77dc25e Mon Sep 17 00:00:00 2001 From: Om Ashishkumar Soni Date: Sun, 1 Oct 2023 23:13:07 +0530 Subject: [PATCH 02/29] added coordinate_compression & doctest --- other/coordinate_compression.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index 935901e32cff..a998ae0de261 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -77,6 +77,14 @@ def compress(self, num): Returns: int: The compressed integer, or -1 if not found in the original list. + + >>> arr = [100, 10, 52, 83] + >>> cc = CoordinateCompressor(arr) + >>> cc.compress(100) + 3 + >>> cc.compress(7) # Value not in the original list + -1 + """ return self.coordinate_map.get(num, -1) @@ -89,8 +97,16 @@ def decompress(self, num): Returns: original value (any) : The original value. + + >>> arr = [100, 10, 52, 83] + >>> cc = CoordinateCompressor(arr) + >>> cc.decompress(0) + 10 + >>> cc.decompress(5) # Compressed coordinate out of range + -1 + """ - return self.reverse_map[num] + return self.reverse_map[num] if num < len(self.reverse_map) else -1 @staticmethod def how_to_use(): From 08ce4044a83c69f2ba40aceda640bdc800cd70e9 Mon Sep 17 00:00:00 2001 From: Om Ashishkumar Soni Date: Mon, 2 Oct 2023 07:11:00 +0530 Subject: [PATCH 03/29] adding return type hints and utilized enumerate --- other/coordinate_compression.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index a998ae0de261..8f49c611adc4 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -33,7 +33,7 @@ class CoordinateCompressor: the list. """ - def __init__(self, arr): + def __init__(self, arr: list) -> None: """ Initialize the CoordinateCompressor with a list. @@ -57,9 +57,19 @@ def __init__(self, arr): self.n = len(arr) # The length of the input list self.compress_coordinates() - def compress_coordinates(self): + def compress_coordinates(self) -> None: """ Compress the coordinates in the input list. + + >>> arr = [100, 10, 52, 83] + >>> cc = CoordinateCompressor(arr) + >>> cc.coordinate_map[83] + 2 + >>> cc.coordinate_map.get(80,-1) # Value not in the original list + -1 + >>> cc.reverse_map[2] # Value not in the original list + 83 + """ key = 0 for val in self.arr: @@ -68,12 +78,12 @@ def compress_coordinates(self): self.reverse_map[key] = val key += 1 - def compress(self, num): + def compress(self, original: any) -> int: """ Compress a single value. Args: - num (any) : The value to compress. + original (any) : The value to compress. Returns: int: The compressed integer, or -1 if not found in the original list. @@ -86,9 +96,9 @@ def compress(self, num): -1 """ - return self.coordinate_map.get(num, -1) + return self.coordinate_map.get(original, -1) - def decompress(self, num): + def decompress(self, num: int) -> any: """ Decompress a single integer. @@ -109,7 +119,7 @@ def decompress(self, num): return self.reverse_map[num] if num < len(self.reverse_map) else -1 @staticmethod - def how_to_use(): + def how_to_use() -> None: """ Example usage of CoordinateCompressor. """ @@ -118,10 +128,10 @@ def how_to_use(): compressed = [0] * len(arr) decompressed = [0] * len(arr) - for i in range(len(arr)): - compressed[i] = cc.compress(arr[i]) + for i, original in enumerate(arr): + compressed[i] = cc.compress(original) decompressed[i] = cc.decompress(compressed[i]) - print(f"Original: {arr[i]}, Compressed: {compressed[i]}") + print(f"Original: {original}, Compressed: {compressed[i]}") if __name__ == "__main__": From cf2194dcb0bed92b42b8d493efb5a2270b42251d Mon Sep 17 00:00:00 2001 From: Om Ashishkumar Soni Date: Mon, 2 Oct 2023 07:19:10 +0530 Subject: [PATCH 04/29] adding exmaple usage in main function --- other/coordinate_compression.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index 8f49c611adc4..25f73f7c81bb 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -118,23 +118,18 @@ def decompress(self, num: int) -> any: """ return self.reverse_map[num] if num < len(self.reverse_map) else -1 - @staticmethod - def how_to_use() -> None: - """ - Example usage of CoordinateCompressor. - """ - arr = [100, 10, 52, 83] - cc = CoordinateCompressor(arr) - compressed = [0] * len(arr) - decompressed = [0] * len(arr) - - for i, original in enumerate(arr): - compressed[i] = cc.compress(original) - decompressed[i] = cc.decompress(compressed[i]) - print(f"Original: {original}, Compressed: {compressed[i]}") - if __name__ == "__main__": from doctest import testmod testmod() + + arr = [100, 10, 52, 83] + cc = CoordinateCompressor(arr) + compressed = [0] * len(arr) + decompressed = [0] * len(arr) + + for i, original in enumerate(arr): + compressed[i] = cc.compress(original) + decompressed[i] = cc.decompress(compressed[i]) + print(f"Original: {original}, Compressed: {compressed[i]}") From 6a0061a28d5b9514fb8925fa1ea290648ca7cb3a Mon Sep 17 00:00:00 2001 From: Om Ashishkumar Soni Date: Mon, 2 Oct 2023 08:05:06 +0530 Subject: [PATCH 05/29] added type hints, for list and dict --- other/coordinate_compression.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index 25f73f7c81bb..923c45c185fb 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -33,7 +33,7 @@ class CoordinateCompressor: the list. """ - def __init__(self, arr: list) -> None: + def __init__(self, arr: list[int | float | str]) -> None: """ Initialize the CoordinateCompressor with a list. @@ -51,8 +51,14 @@ def __init__(self, arr: list) -> None: """ - self.coordinate_map = {} # A dictionary to store compressed coordinates - self.reverse_map = [-1] * (len(arr)) # A list to store reverse mapping + self.coordinate_map: dict[ + int | float | str, int + ] = {} # A dictionary to store compressed coordinates + + self.reverse_map: list[int | float | str] = [-1] * ( + len(arr) + ) # A list to store reverse mapping + self.arr = sorted(arr) # The input list self.n = len(arr) # The length of the input list self.compress_coordinates() @@ -78,12 +84,12 @@ def compress_coordinates(self) -> None: self.reverse_map[key] = val key += 1 - def compress(self, original: any) -> int: + def compress(self, original: float | str) -> int: """ Compress a single value. Args: - original (any) : The value to compress. + original (int | float | str) : The value to compress. Returns: int: The compressed integer, or -1 if not found in the original list. @@ -98,7 +104,7 @@ def compress(self, original: any) -> int: """ return self.coordinate_map.get(original, -1) - def decompress(self, num: int) -> any: + def decompress(self, num: int) -> int | float | str: """ Decompress a single integer. @@ -106,7 +112,7 @@ def decompress(self, num: int) -> any: num (int): The compressed integer to decompress. Returns: - original value (any) : The original value. + original value (int | float | str) : The original value. >>> arr = [100, 10, 52, 83] >>> cc = CoordinateCompressor(arr) @@ -124,10 +130,10 @@ def decompress(self, num: int) -> any: testmod() - arr = [100, 10, 52, 83] + arr: list[int | float | str] = [100, 10, 52, 83] cc = CoordinateCompressor(arr) - compressed = [0] * len(arr) - decompressed = [0] * len(arr) + compressed: list[int] = [0] * len(arr) + decompressed: list[int | float | str] = [0] * len(arr) for i, original in enumerate(arr): compressed[i] = cc.compress(original) From 1b327731b2aa5cb450cb6f1835a1a7cfe5cf2c12 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Wed, 27 Aug 2025 18:25:40 +0000 Subject: [PATCH 06/29] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 53c53d208656..78af5000777e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -854,6 +854,7 @@ * [Activity Selection](other/activity_selection.py) * [Alternative List Arrange](other/alternative_list_arrange.py) * [Bankers Algorithm](other/bankers_algorithm.py) + * [Coordinate Compression](other/coordinate_compression.py) * [Davis Putnam Logemann Loveland](other/davis_putnam_logemann_loveland.py) * [Doomsday](other/doomsday.py) * [Fischer Yates Shuffle](other/fischer_yates_shuffle.py) From cee48d27c80be4621a03d3b8db51671e685969c3 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:26:07 +0300 Subject: [PATCH 07/29] Update other/coordinate_compression.py Co-authored-by: Christian Clauss --- other/coordinate_compression.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index 923c45c185fb..e928ed5bc34a 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -48,16 +48,13 @@ def __init__(self, arr: list[int | float | str]) -> None: 1 >>> cc.decompress(1) 52 - """ - self.coordinate_map: dict[ - int | float | str, int - ] = {} # A dictionary to store compressed coordinates + # A dictionary to store compressed coordinates + self.coordinate_map: dict[int | float | str, int] = {} - self.reverse_map: list[int | float | str] = [-1] * ( - len(arr) - ) # A list to store reverse mapping + # A list to store reverse mapping + self.reverse_map: list[int | float | str] = [-1] * len(arr) self.arr = sorted(arr) # The input list self.n = len(arr) # The length of the input list From eb9c481e1115ff811f060f867543dca0b263c64e Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:26:26 +0300 Subject: [PATCH 08/29] Update other/coordinate_compression.py Co-authored-by: Christian Clauss --- other/coordinate_compression.py | 1 - 1 file changed, 1 deletion(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index e928ed5bc34a..c0d4551b0110 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -72,7 +72,6 @@ def compress_coordinates(self) -> None: -1 >>> cc.reverse_map[2] # Value not in the original list 83 - """ key = 0 for val in self.arr: From bcf4f0dd2086866fbc1674b90c203d4ac3018f6b Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:26:33 +0300 Subject: [PATCH 09/29] Update other/coordinate_compression.py Co-authored-by: Christian Clauss --- other/coordinate_compression.py | 1 - 1 file changed, 1 deletion(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index c0d4551b0110..765364096f72 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -96,7 +96,6 @@ def compress(self, original: float | str) -> int: 3 >>> cc.compress(7) # Value not in the original list -1 - """ return self.coordinate_map.get(original, -1) From 0e32489cb06ef882d2fe325c4bec388a5af23e61 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:26:48 +0300 Subject: [PATCH 10/29] Update other/coordinate_compression.py Co-authored-by: Christian Clauss --- other/coordinate_compression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index 765364096f72..ef1e2a1266a5 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -38,7 +38,7 @@ def __init__(self, arr: list[int | float | str]) -> None: Initialize the CoordinateCompressor with a list. Args: - arr (list): The list of values to be compressed. + arr: The list of values to be compressed. >>> arr = [100, 10, 52, 83] >>> cc = CoordinateCompressor(arr) From 986931fb6ae110544c61c5b89012387df3ec23a7 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:27:03 +0300 Subject: [PATCH 11/29] Update other/coordinate_compression.py Co-authored-by: Christian Clauss --- other/coordinate_compression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index ef1e2a1266a5..ea5aeaceb009 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -85,7 +85,7 @@ def compress(self, original: float | str) -> int: Compress a single value. Args: - original (int | float | str) : The value to compress. + original: The value to compress. Returns: int: The compressed integer, or -1 if not found in the original list. From 291e259dad47296860d9e06673758fcd98df3adf Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:27:13 +0300 Subject: [PATCH 12/29] Update other/coordinate_compression.py Co-authored-by: Christian Clauss --- other/coordinate_compression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index ea5aeaceb009..3204615b9a3d 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -104,7 +104,7 @@ def decompress(self, num: int) -> int | float | str: Decompress a single integer. Args: - num (int): The compressed integer to decompress. + num: The compressed integer to decompress. Returns: original value (int | float | str) : The original value. From dba753e5f3af589bd129c12ffe674eabc42b514a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 Aug 2025 18:27:33 +0000 Subject: [PATCH 13/29] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/coordinate_compression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index 3204615b9a3d..0b566e5ab82a 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -51,7 +51,7 @@ def __init__(self, arr: list[int | float | str]) -> None: """ # A dictionary to store compressed coordinates - self.coordinate_map: dict[int | float | str, int] = {} + self.coordinate_map: dict[int | float | str, int] = {} # A list to store reverse mapping self.reverse_map: list[int | float | str] = [-1] * len(arr) From 6a3c6425fb5e4b2763a2c0f47fbd5000f912dc7d Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:28:03 +0300 Subject: [PATCH 14/29] Update other/coordinate_compression.py Co-authored-by: Christian Clauss --- other/coordinate_compression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index 0b566e5ab82a..1f1757074416 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -107,7 +107,7 @@ def decompress(self, num: int) -> int | float | str: num: The compressed integer to decompress. Returns: - original value (int | float | str) : The original value. + The original value. >>> arr = [100, 10, 52, 83] >>> cc = CoordinateCompressor(arr) From 446cbe257ba39b559ec5533aceebddb2ead3c8ce Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:28:09 +0300 Subject: [PATCH 15/29] Update other/coordinate_compression.py Co-authored-by: Christian Clauss --- other/coordinate_compression.py | 1 - 1 file changed, 1 deletion(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index 1f1757074416..bf822c5ae069 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -115,7 +115,6 @@ def decompress(self, num: int) -> int | float | str: 10 >>> cc.decompress(5) # Compressed coordinate out of range -1 - """ return self.reverse_map[num] if num < len(self.reverse_map) else -1 From f8ccfe84d9e42faca895f8f7e2b91af6ad5f25ab Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:30:01 +0300 Subject: [PATCH 16/29] Update coordinate_compression.py --- other/coordinate_compression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py index bf822c5ae069..9b5ec1bac702 100644 --- a/other/coordinate_compression.py +++ b/other/coordinate_compression.py @@ -88,7 +88,7 @@ def compress(self, original: float | str) -> int: original: The value to compress. Returns: - int: The compressed integer, or -1 if not found in the original list. + The compressed integer, or -1 if not found in the original list. >>> arr = [100, 10, 52, 83] >>> cc = CoordinateCompressor(arr) From a008eb5e784283abaf1b2413e13169014c8d6294 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:37:03 +0300 Subject: [PATCH 17/29] Create coordinate_compression.py --- data_compression/coordinate_compression.py | 135 +++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 data_compression/coordinate_compression.py diff --git a/data_compression/coordinate_compression.py b/data_compression/coordinate_compression.py new file mode 100644 index 000000000000..9b5ec1bac702 --- /dev/null +++ b/data_compression/coordinate_compression.py @@ -0,0 +1,135 @@ +""" +Assumption: + - The values to compress are assumed to be comparable, + - values can be sorted and compared with '<' and '>' operators. +""" + + +class CoordinateCompressor: + """ + A class for coordinate compression. + + This class allows you to compress and decompress a list of values. + + Mapping: + In addition to compression and decompression, this class maintains a mapping + between original values and their compressed counterparts using two data + structures: a dictionary `coordinate_map` and a list `reverse_map`. + + - `coordinate_map`: A dictionary that maps original values to their compressed + coordinates. Keys are original values, and values are compressed coordinates. + + - `reverse_map`: A list used for reverse mapping, where each index corresponds + to a compressed coordinate, and the value at that index is the original value. + + Example mapping: + + Original: 10, Compressed: 0 + Original: 52, Compressed: 1 + Original: 83, Compressed: 2 + Original: 100, Compressed: 3 + + This mapping allows for efficient compression and decompression of values within + the list. + """ + + def __init__(self, arr: list[int | float | str]) -> None: + """ + Initialize the CoordinateCompressor with a list. + + Args: + arr: The list of values to be compressed. + + >>> arr = [100, 10, 52, 83] + >>> cc = CoordinateCompressor(arr) + >>> cc.compress(100) + 3 + >>> cc.compress(52) + 1 + >>> cc.decompress(1) + 52 + """ + + # A dictionary to store compressed coordinates + self.coordinate_map: dict[int | float | str, int] = {} + + # A list to store reverse mapping + self.reverse_map: list[int | float | str] = [-1] * len(arr) + + self.arr = sorted(arr) # The input list + self.n = len(arr) # The length of the input list + self.compress_coordinates() + + def compress_coordinates(self) -> None: + """ + Compress the coordinates in the input list. + + >>> arr = [100, 10, 52, 83] + >>> cc = CoordinateCompressor(arr) + >>> cc.coordinate_map[83] + 2 + >>> cc.coordinate_map.get(80,-1) # Value not in the original list + -1 + >>> cc.reverse_map[2] # Value not in the original list + 83 + """ + key = 0 + for val in self.arr: + if val not in self.coordinate_map: + self.coordinate_map[val] = key + self.reverse_map[key] = val + key += 1 + + def compress(self, original: float | str) -> int: + """ + Compress a single value. + + Args: + original: The value to compress. + + Returns: + The compressed integer, or -1 if not found in the original list. + + >>> arr = [100, 10, 52, 83] + >>> cc = CoordinateCompressor(arr) + >>> cc.compress(100) + 3 + >>> cc.compress(7) # Value not in the original list + -1 + """ + return self.coordinate_map.get(original, -1) + + def decompress(self, num: int) -> int | float | str: + """ + Decompress a single integer. + + Args: + num: The compressed integer to decompress. + + Returns: + The original value. + + >>> arr = [100, 10, 52, 83] + >>> cc = CoordinateCompressor(arr) + >>> cc.decompress(0) + 10 + >>> cc.decompress(5) # Compressed coordinate out of range + -1 + """ + return self.reverse_map[num] if num < len(self.reverse_map) else -1 + + +if __name__ == "__main__": + from doctest import testmod + + testmod() + + arr: list[int | float | str] = [100, 10, 52, 83] + cc = CoordinateCompressor(arr) + compressed: list[int] = [0] * len(arr) + decompressed: list[int | float | str] = [0] * len(arr) + + for i, original in enumerate(arr): + compressed[i] = cc.compress(original) + decompressed[i] = cc.decompress(compressed[i]) + print(f"Original: {original}, Compressed: {compressed[i]}") From 05a8348442aefaf60fd35c1ba83b04defae32d74 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Wed, 27 Aug 2025 18:37:11 +0000 Subject: [PATCH 18/29] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 78af5000777e..d4c94e495806 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -174,6 +174,7 @@ ## Data Compression * [Burrows Wheeler](data_compression/burrows_wheeler.py) + * [Coordinate Compression](data_compression/coordinate_compression.py) * [Huffman](data_compression/huffman.py) * [Lempel Ziv](data_compression/lempel_ziv.py) * [Lempel Ziv Decompress](data_compression/lempel_ziv_decompress.py) From 2be687accc5c8daa9247abaa4cbc3dfa70df208e Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:37:21 +0300 Subject: [PATCH 19/29] Delete other/coordinate_compression.py --- other/coordinate_compression.py | 135 -------------------------------- 1 file changed, 135 deletions(-) delete mode 100644 other/coordinate_compression.py diff --git a/other/coordinate_compression.py b/other/coordinate_compression.py deleted file mode 100644 index 9b5ec1bac702..000000000000 --- a/other/coordinate_compression.py +++ /dev/null @@ -1,135 +0,0 @@ -""" -Assumption: - - The values to compress are assumed to be comparable, - - values can be sorted and compared with '<' and '>' operators. -""" - - -class CoordinateCompressor: - """ - A class for coordinate compression. - - This class allows you to compress and decompress a list of values. - - Mapping: - In addition to compression and decompression, this class maintains a mapping - between original values and their compressed counterparts using two data - structures: a dictionary `coordinate_map` and a list `reverse_map`. - - - `coordinate_map`: A dictionary that maps original values to their compressed - coordinates. Keys are original values, and values are compressed coordinates. - - - `reverse_map`: A list used for reverse mapping, where each index corresponds - to a compressed coordinate, and the value at that index is the original value. - - Example mapping: - - Original: 10, Compressed: 0 - Original: 52, Compressed: 1 - Original: 83, Compressed: 2 - Original: 100, Compressed: 3 - - This mapping allows for efficient compression and decompression of values within - the list. - """ - - def __init__(self, arr: list[int | float | str]) -> None: - """ - Initialize the CoordinateCompressor with a list. - - Args: - arr: The list of values to be compressed. - - >>> arr = [100, 10, 52, 83] - >>> cc = CoordinateCompressor(arr) - >>> cc.compress(100) - 3 - >>> cc.compress(52) - 1 - >>> cc.decompress(1) - 52 - """ - - # A dictionary to store compressed coordinates - self.coordinate_map: dict[int | float | str, int] = {} - - # A list to store reverse mapping - self.reverse_map: list[int | float | str] = [-1] * len(arr) - - self.arr = sorted(arr) # The input list - self.n = len(arr) # The length of the input list - self.compress_coordinates() - - def compress_coordinates(self) -> None: - """ - Compress the coordinates in the input list. - - >>> arr = [100, 10, 52, 83] - >>> cc = CoordinateCompressor(arr) - >>> cc.coordinate_map[83] - 2 - >>> cc.coordinate_map.get(80,-1) # Value not in the original list - -1 - >>> cc.reverse_map[2] # Value not in the original list - 83 - """ - key = 0 - for val in self.arr: - if val not in self.coordinate_map: - self.coordinate_map[val] = key - self.reverse_map[key] = val - key += 1 - - def compress(self, original: float | str) -> int: - """ - Compress a single value. - - Args: - original: The value to compress. - - Returns: - The compressed integer, or -1 if not found in the original list. - - >>> arr = [100, 10, 52, 83] - >>> cc = CoordinateCompressor(arr) - >>> cc.compress(100) - 3 - >>> cc.compress(7) # Value not in the original list - -1 - """ - return self.coordinate_map.get(original, -1) - - def decompress(self, num: int) -> int | float | str: - """ - Decompress a single integer. - - Args: - num: The compressed integer to decompress. - - Returns: - The original value. - - >>> arr = [100, 10, 52, 83] - >>> cc = CoordinateCompressor(arr) - >>> cc.decompress(0) - 10 - >>> cc.decompress(5) # Compressed coordinate out of range - -1 - """ - return self.reverse_map[num] if num < len(self.reverse_map) else -1 - - -if __name__ == "__main__": - from doctest import testmod - - testmod() - - arr: list[int | float | str] = [100, 10, 52, 83] - cc = CoordinateCompressor(arr) - compressed: list[int] = [0] * len(arr) - decompressed: list[int | float | str] = [0] * len(arr) - - for i, original in enumerate(arr): - compressed[i] = cc.compress(original) - decompressed[i] = cc.decompress(compressed[i]) - print(f"Original: {original}, Compressed: {compressed[i]}") From 2b9579742b7b913f771f5cd7ee26def3a792453c Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Wed, 27 Aug 2025 18:37:30 +0000 Subject: [PATCH 20/29] updating DIRECTORY.md --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index d4c94e495806..0636ba0a7ecc 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -855,7 +855,6 @@ * [Activity Selection](other/activity_selection.py) * [Alternative List Arrange](other/alternative_list_arrange.py) * [Bankers Algorithm](other/bankers_algorithm.py) - * [Coordinate Compression](other/coordinate_compression.py) * [Davis Putnam Logemann Loveland](other/davis_putnam_logemann_loveland.py) * [Doomsday](other/doomsday.py) * [Fischer Yates Shuffle](other/fischer_yates_shuffle.py) From 56cf8c4de4ec6948b8ca4bddbed394a4e07bdb0b Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:40:03 +0300 Subject: [PATCH 21/29] Update coordinate_compression.py --- data_compression/coordinate_compression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_compression/coordinate_compression.py b/data_compression/coordinate_compression.py index 9b5ec1bac702..d6f368bcdbe0 100644 --- a/data_compression/coordinate_compression.py +++ b/data_compression/coordinate_compression.py @@ -1,7 +1,7 @@ """ Assumption: - The values to compress are assumed to be comparable, - - values can be sorted and compared with '<' and '>' operators. + values can be sorted and compared with '<' and '>' operators. """ From c5efe8698d4cec8494ecf8936b30a2428e160740 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:41:36 +0300 Subject: [PATCH 22/29] Update coordinate_compression.py --- data_compression/coordinate_compression.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/data_compression/coordinate_compression.py b/data_compression/coordinate_compression.py index d6f368bcdbe0..7283ad72eff7 100644 --- a/data_compression/coordinate_compression.py +++ b/data_compression/coordinate_compression.py @@ -14,11 +14,9 @@ class CoordinateCompressor: Mapping: In addition to compression and decompression, this class maintains a mapping between original values and their compressed counterparts using two data - structures: a dictionary `coordinate_map` and a list `reverse_map`. - + structures: a dictionary `coordinate_map` and a list `reverse_map`: - `coordinate_map`: A dictionary that maps original values to their compressed coordinates. Keys are original values, and values are compressed coordinates. - - `reverse_map`: A list used for reverse mapping, where each index corresponds to a compressed coordinate, and the value at that index is the original value. From fa07c59746b5c106e0f7122d839b6209dd086fe7 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:43:01 +0300 Subject: [PATCH 23/29] Update coordinate_compression.py --- data_compression/coordinate_compression.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data_compression/coordinate_compression.py b/data_compression/coordinate_compression.py index 7283ad72eff7..8db01cabb707 100644 --- a/data_compression/coordinate_compression.py +++ b/data_compression/coordinate_compression.py @@ -20,8 +20,7 @@ class CoordinateCompressor: - `reverse_map`: A list used for reverse mapping, where each index corresponds to a compressed coordinate, and the value at that index is the original value. - Example mapping: - + Example of mapping: Original: 10, Compressed: 0 Original: 52, Compressed: 1 Original: 83, Compressed: 2 From e916fc7cb6fcbb2d01ee092149cb2db12b4939b8 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:46:35 +0300 Subject: [PATCH 24/29] Update coordinate_compression.py --- data_compression/coordinate_compression.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_compression/coordinate_compression.py b/data_compression/coordinate_compression.py index 8db01cabb707..d3ec51d5af7a 100644 --- a/data_compression/coordinate_compression.py +++ b/data_compression/coordinate_compression.py @@ -65,9 +65,9 @@ def compress_coordinates(self) -> None: >>> cc = CoordinateCompressor(arr) >>> cc.coordinate_map[83] 2 - >>> cc.coordinate_map.get(80,-1) # Value not in the original list + >>> cc.coordinate_map[80] # Value not in the original list -1 - >>> cc.reverse_map[2] # Value not in the original list + >>> cc.reverse_map[2] 83 """ key = 0 From 8b99d69fef9fdf6e7bc2ed4d9e06b18d6121cd79 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:50:00 +0300 Subject: [PATCH 25/29] Update coordinate_compression.py --- data_compression/coordinate_compression.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/data_compression/coordinate_compression.py b/data_compression/coordinate_compression.py index d3ec51d5af7a..5ecfdd637abd 100644 --- a/data_compression/coordinate_compression.py +++ b/data_compression/coordinate_compression.py @@ -66,7 +66,9 @@ def compress_coordinates(self) -> None: >>> cc.coordinate_map[83] 2 >>> cc.coordinate_map[80] # Value not in the original list - -1 + Traceback (most recent call last): + ... + KeyError: 80 >>> cc.reverse_map[2] 83 """ From f48981d9d8c404cbe49ca62906b5f6ef0493d7a7 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:51:22 +0300 Subject: [PATCH 26/29] Update coordinate_compression.py --- data_compression/coordinate_compression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_compression/coordinate_compression.py b/data_compression/coordinate_compression.py index 5ecfdd637abd..f4b545f9e90f 100644 --- a/data_compression/coordinate_compression.py +++ b/data_compression/coordinate_compression.py @@ -79,7 +79,7 @@ def compress_coordinates(self) -> None: self.reverse_map[key] = val key += 1 - def compress(self, original: float | str) -> int: + def compress(self, original: int | float | str) -> int: """ Compress a single value. From 7469f71f671a95e34fe0865f18ac652c596557a7 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:53:43 +0300 Subject: [PATCH 27/29] Update coordinate_compression.py --- data_compression/coordinate_compression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_compression/coordinate_compression.py b/data_compression/coordinate_compression.py index f4b545f9e90f..7f53c34a0637 100644 --- a/data_compression/coordinate_compression.py +++ b/data_compression/coordinate_compression.py @@ -115,7 +115,7 @@ def decompress(self, num: int) -> int | float | str: >>> cc.decompress(5) # Compressed coordinate out of range -1 """ - return self.reverse_map[num] if num < len(self.reverse_map) else -1 + return self.reverse_map[num] if 0 <= num < len(self.reverse_map) else -1 if __name__ == "__main__": From 283d0d030b964018771ce269b2c3cd19ec1dd943 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:57:30 +0300 Subject: [PATCH 28/29] Update coordinate_compression.py --- data_compression/coordinate_compression.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/data_compression/coordinate_compression.py b/data_compression/coordinate_compression.py index 7f53c34a0637..09eec85d64b9 100644 --- a/data_compression/coordinate_compression.py +++ b/data_compression/coordinate_compression.py @@ -125,10 +125,8 @@ def decompress(self, num: int) -> int | float | str: arr: list[int | float | str] = [100, 10, 52, 83] cc = CoordinateCompressor(arr) - compressed: list[int] = [0] * len(arr) - decompressed: list[int | float | str] = [0] * len(arr) - for i, original in enumerate(arr): - compressed[i] = cc.compress(original) - decompressed[i] = cc.decompress(compressed[i]) - print(f"Original: {original}, Compressed: {compressed[i]}") + for original in arr: + compressed = cc.compress(original) + decompressed = cc.decompress(compressed) + print(f"Original: {decompressed}, Compressed: {compressed}") From fbca0f556dc2c2ef6a59a872cba9ea2c4a0c258d Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Aug 2025 21:59:47 +0300 Subject: [PATCH 29/29] Update coordinate_compression.py --- data_compression/coordinate_compression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_compression/coordinate_compression.py b/data_compression/coordinate_compression.py index 09eec85d64b9..9c4ad9a99ac3 100644 --- a/data_compression/coordinate_compression.py +++ b/data_compression/coordinate_compression.py @@ -79,7 +79,7 @@ def compress_coordinates(self) -> None: self.reverse_map[key] = val key += 1 - def compress(self, original: int | float | str) -> int: + def compress(self, original: float | str) -> int: """ Compress a single value.