Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
35 changes: 29 additions & 6 deletions data_compression/coordinate_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,28 @@ def compress(self, original: float | str) -> int:
Args:
original: The value to compress.

# Returns:
# The compressed integer, or -1 if not found in the original list.

Returns:
The compressed integer, or -1 if not found in the original list.
The compressed integer.

Raises:
KeyError: If the value is not present 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
Traceback (most recent call last):
...
KeyError: 7
"""
return self.coordinate_map.get(original, -1)
# return self.coordinate_map.get(original, -1)
if original not in self.coordinate_map:
raise KeyError(original)
return self.coordinate_map[original]

def decompress(self, num: int) -> int | float | str:
"""
Expand All @@ -105,17 +116,29 @@ def decompress(self, num: int) -> int | float | str:
Args:
num: The compressed integer to decompress.

# Returns:
# The original value.

Returns:
The original value.
The original value.

Raises:
KeyError: If the compressed coordinate is out of range.

>>> arr = [100, 10, 52, 83]
>>> cc = CoordinateCompressor(arr)
>>> cc.decompress(0)
10
>>> cc.decompress(5) # Compressed coordinate out of range
-1
Traceback (most recent call last):
...
KeyError: 5

"""
return self.reverse_map[num] if 0 <= num < len(self.reverse_map) else -1
# return self.reverse_map[num] if 0 <= num < len(self.reverse_map) else -1
if not 0 <= num < len(self.reverse_map):
raise KeyError(num)
return self.reverse_map[num]


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion data_structures/hashing/hash_table_with_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def _set_value(self, key, data):
self.values[key] = deque([]) if self.values[key] is None else self.values[key]
self.values[key] = deque() if self.values[key] is None else self.values[key]
self.values[key].appendleft(data)
self._keys[key] = self.values[key]

Expand Down
10 changes: 5 additions & 5 deletions data_structures/trie/radix_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,10 @@ def insert(self, word: str) -> None:
"""
# Case 1: If the word is the prefix of the node
# Solution: We set the current node as leaf
if self.prefix == word and not self.is_leaf:
self.is_leaf = True

# Case 2: The node has no edges that have a prefix to the word
# Solution: We create an edge from the current node to a new one
# containing the word
elif word[0] not in self.nodes:
if word[0] not in self.nodes:
self.nodes[word[0]] = RadixNode(prefix=word, is_leaf=True)

else:
Expand All @@ -82,7 +79,10 @@ def insert(self, word: str) -> None:
# Case 3: The node prefix is equal to the matching
# Solution: We insert remaining word on the next node
if remaining_prefix == "":
self.nodes[matching_string[0]].insert(remaining_word)
if remaining_word == "":
incoming_node.is_leaf = True
else:
incoming_node.insert(remaining_word)

# Case 4: The word is greater equal to the matching
# Solution: Create a node in between both nodes, change
Expand Down
Loading