From 04644e19c9b5b592634a82f5f85dd11ea736bd41 Mon Sep 17 00:00:00 2001 From: Tridip Kalita <147252942+tri-23-dip@users.noreply.github.com> Date: Fri, 13 Feb 2026 13:56:34 +0530 Subject: [PATCH 1/5] Remove unreachable Case 1 in radix tree insert and handle exact match inside Case 3 --- data_structures/trie/radix_tree.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index bd2306befa79..75ee10df7cd8 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -64,13 +64,13 @@ 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 + # if self.prefix == word and not self.is_leaf: -------------------------------issue fix + # self.is_leaf = True --------------------------------------------------------------issue fix # 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 +82,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 From 4f7587eef6c3f8af6ec7c876b3e567463e14552a Mon Sep 17 00:00:00 2001 From: Tridip Kalita <147252942+tri-23-dip@users.noreply.github.com> Date: Sat, 14 Feb 2026 14:49:47 +0530 Subject: [PATCH 2/5] Fix: Ensure consistent KeyError handling for missing values in CoordinateCompressor (#13226) --- data_compression/coordinate_compression.py | 35 ++++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) 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__": From 7a917783f61f1da4d49353c9a81ad9204d2fa9aa Mon Sep 17 00:00:00 2001 From: Tridip Kalita <147252942+tri-23-dip@users.noreply.github.com> Date: Sat, 14 Feb 2026 16:47:36 +0530 Subject: [PATCH 3/5] Fix E501 line length in radix_tree --- data_structures/trie/radix_tree.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index 75ee10df7cd8..373e67a6131b 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -64,9 +64,7 @@ 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: -------------------------------issue fix - # self.is_leaf = True --------------------------------------------------------------issue fix - + # 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 From e74f12715a53493c065b8dfddab6581a1f7869d6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Feb 2026 11:18:07 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/trie/radix_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index 373e67a6131b..a7e0ad39f0e5 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -64,7 +64,7 @@ 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 - + # 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 From 91bf45e9ac0141ebe2aa968368ad0f55fd8d6f19 Mon Sep 17 00:00:00 2001 From: Tridip Kalita <147252942+tri-23-dip@users.noreply.github.com> Date: Sat, 14 Feb 2026 17:00:05 +0530 Subject: [PATCH 5/5] Fix ruff issues (deque and whitespace) --- data_structures/hashing/hash_table_with_linked_list.py | 2 +- data_structures/trie/radix_tree.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) 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 a7e0ad39f0e5..7879cbcd3af9 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -64,7 +64,6 @@ 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 - # 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