Skip to content
Merged
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d7fecbe
added coordinate_compression algorithm
om-ashish-soni Oct 1, 2023
767e4a5
added coordinate_compression & doctest
om-ashish-soni Oct 1, 2023
08ce404
adding return type hints and utilized enumerate
om-ashish-soni Oct 2, 2023
cf2194d
adding exmaple usage in main function
om-ashish-soni Oct 2, 2023
6a0061a
added type hints, for list and dict
om-ashish-soni Oct 2, 2023
3e3530e
Merge branch 'master' into hacktoberfest-2023
MaximSmolskiy Aug 27, 2025
1b32773
updating DIRECTORY.md
MaximSmolskiy Aug 27, 2025
cee48d2
Update other/coordinate_compression.py
MaximSmolskiy Aug 27, 2025
eb9c481
Update other/coordinate_compression.py
MaximSmolskiy Aug 27, 2025
bcf4f0d
Update other/coordinate_compression.py
MaximSmolskiy Aug 27, 2025
0e32489
Update other/coordinate_compression.py
MaximSmolskiy Aug 27, 2025
986931f
Update other/coordinate_compression.py
MaximSmolskiy Aug 27, 2025
291e259
Update other/coordinate_compression.py
MaximSmolskiy Aug 27, 2025
dba753e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 27, 2025
6a3c642
Update other/coordinate_compression.py
MaximSmolskiy Aug 27, 2025
446cbe2
Update other/coordinate_compression.py
MaximSmolskiy Aug 27, 2025
f8ccfe8
Update coordinate_compression.py
MaximSmolskiy Aug 27, 2025
a008eb5
Create coordinate_compression.py
MaximSmolskiy Aug 27, 2025
05a8348
updating DIRECTORY.md
MaximSmolskiy Aug 27, 2025
2be687a
Delete other/coordinate_compression.py
MaximSmolskiy Aug 27, 2025
2b95797
updating DIRECTORY.md
MaximSmolskiy Aug 27, 2025
56cf8c4
Update coordinate_compression.py
MaximSmolskiy Aug 27, 2025
c5efe86
Update coordinate_compression.py
MaximSmolskiy Aug 27, 2025
fa07c59
Update coordinate_compression.py
MaximSmolskiy Aug 27, 2025
e916fc7
Update coordinate_compression.py
MaximSmolskiy Aug 27, 2025
8b99d69
Update coordinate_compression.py
MaximSmolskiy Aug 27, 2025
f48981d
Update coordinate_compression.py
MaximSmolskiy Aug 27, 2025
7469f71
Update coordinate_compression.py
MaximSmolskiy Aug 27, 2025
283d0d0
Update coordinate_compression.py
MaximSmolskiy Aug 27, 2025
fbca0f5
Update coordinate_compression.py
MaximSmolskiy Aug 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions other/coordinate_compression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
"""
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought... If this is ints on the left and ints on the right, how is a one-to-one mapping compressing anything? Would a dict be as efficient as a dict in this use case?


This mapping allows for efficient compression and decompression of values within
the list.
"""

def __init__(self, arr: list) -> None:
"""
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) -> 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: any) -> int:
"""
Compress a single value.

Args:
original (any) : The value to compress.

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(original, -1)

def decompress(self, num: int) -> any:
"""
Decompress a single integer.

Args:
num (int): The compressed integer to decompress.

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] 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()