diff --git a/data_compression/coordinate_compression.py b/data_compression/coordinate_compression.py index 9c4ad9a99ac3..87a215355687 100644 --- a/data_compression/coordinate_compression.py +++ b/data_compression/coordinate_compression.py @@ -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: """ @@ -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__": diff --git a/data_structures/hashing/hash_table_with_linked_list.py b/data_structures/hashing/hash_table_with_linked_list.py index f404c5251246..c8dffa30b8e8 100644 --- a/data_structures/hashing/hash_table_with_linked_list.py +++ b/data_structures/hashing/hash_table_with_linked_list.py @@ -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] diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index bd2306befa79..7879cbcd3af9 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -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: @@ -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