-
-
Notifications
You must be signed in to change notification settings - Fork 47.7k
added coordinate_compression #9317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MaximSmolskiy
merged 30 commits into
TheAlgorithms:master
from
om-ashish-soni:hacktoberfest-2023
Aug 27, 2025
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 767e4a5
added coordinate_compression & doctest
om-ashish-soni 08ce404
adding return type hints and utilized enumerate
om-ashish-soni cf2194d
adding exmaple usage in main function
om-ashish-soni 6a0061a
added type hints, for list and dict
om-ashish-soni 3e3530e
Merge branch 'master' into hacktoberfest-2023
MaximSmolskiy 1b32773
updating DIRECTORY.md
MaximSmolskiy cee48d2
Update other/coordinate_compression.py
MaximSmolskiy eb9c481
Update other/coordinate_compression.py
MaximSmolskiy bcf4f0d
Update other/coordinate_compression.py
MaximSmolskiy 0e32489
Update other/coordinate_compression.py
MaximSmolskiy 986931f
Update other/coordinate_compression.py
MaximSmolskiy 291e259
Update other/coordinate_compression.py
MaximSmolskiy dba753e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6a3c642
Update other/coordinate_compression.py
MaximSmolskiy 446cbe2
Update other/coordinate_compression.py
MaximSmolskiy f8ccfe8
Update coordinate_compression.py
MaximSmolskiy a008eb5
Create coordinate_compression.py
MaximSmolskiy 05a8348
updating DIRECTORY.md
MaximSmolskiy 2be687a
Delete other/coordinate_compression.py
MaximSmolskiy 2b95797
updating DIRECTORY.md
MaximSmolskiy 56cf8c4
Update coordinate_compression.py
MaximSmolskiy c5efe86
Update coordinate_compression.py
MaximSmolskiy fa07c59
Update coordinate_compression.py
MaximSmolskiy e916fc7
Update coordinate_compression.py
MaximSmolskiy 8b99d69
Update coordinate_compression.py
MaximSmolskiy f48981d
Update coordinate_compression.py
MaximSmolskiy 7469f71
Update coordinate_compression.py
MaximSmolskiy 283d0d0
Update coordinate_compression.py
MaximSmolskiy fbca0f5
Update coordinate_compression.py
MaximSmolskiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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. | ||
MaximSmolskiy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>>> 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 | ||
|
||
MaximSmolskiy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
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 | ||
|
||
MaximSmolskiy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
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. | ||
MaximSmolskiy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
|
||
MaximSmolskiy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
return self.reverse_map[num] if num < len(self.reverse_map) else -1 | ||
|
||
@staticmethod | ||
def how_to_use() -> None: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?