Skip to content

Commit 11cf5b1

Browse files
Fixed doctest
1 parent 0baa6e4 commit 11cf5b1

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

bit_manipulation/max_xor_bit_trie.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TrieNode:
1818
"""Node of the Bitwise Trie."""
1919

2020
def __init__(self) -> None:
21-
self.child: list[TrieNode | None] = [None, None]
21+
self.child: list[TrieNode | None] = [None, None] # child[0] for bit 0, child[1] for bit 1
2222

2323

2424
class BitwiseTrieMaxXOR:
@@ -42,7 +42,7 @@ def insert(self, num: int) -> None:
4242
node = self.root
4343
for i in range(31, -1, -1): # 32-bit integers
4444
bit = (num >> i) & 1
45-
if node.child[bit] is None:
45+
if not node.child[bit]:
4646
node.child[bit] = TrieNode()
4747
node = node.child[bit]
4848

@@ -69,7 +69,7 @@ def query_max_xor(self, num: int) -> int:
6969
for i in range(31, -1, -1):
7070
bit = (num >> i) & 1
7171
toggle = 1 - bit
72-
if node.child[toggle] is not None:
72+
if node.child[toggle]:
7373
max_xor |= 1 << i
7474
node = node.child[toggle]
7575
else:
@@ -80,15 +80,22 @@ def find_maximum_xor(self, nums: list[int]) -> int:
8080
"""
8181
Compute maximum XOR of any two numbers in `nums`.
8282
83-
>>> solver = BitwiseTrieMaxXOR()
84-
>>> solver.find_maximum_xor([3, 10, 5, 25, 2, 8])
83+
>>> BitwiseTrieMaxXOR().find_maximum_xor([3, 10, 5, 25, 2, 8])
8584
28
86-
>>> solver.find_maximum_xor([42])
85+
>>> BitwiseTrieMaxXOR().find_maximum_xor([42])
8786
0
88-
>>> solver.find_maximum_xor([8, 1])
89-
9
90-
>>> solver.find_maximum_xor([0, 0, 0])
87+
>>> BitwiseTrieMaxXOR().find_maximum_xor([0xFFFFFFFF, 0])
88+
4294967295
89+
>>> BitwiseTrieMaxXOR().find_maximum_xor([7, 7, 7])
9190
0
91+
>>> BitwiseTrieMaxXOR().find_maximum_xor([1, 2, 3, 4, 5])
92+
7
93+
>>> BitwiseTrieMaxXOR().find_maximum_xor([16, 8, 4, 2, 1])
94+
24
95+
>>> BitwiseTrieMaxXOR().find_maximum_xor([1, 2, 4, 8, 16, 32])
96+
48
97+
>>> BitwiseTrieMaxXOR().find_maximum_xor([9, 14, 3, 6, 12])
98+
15
9299
"""
93100
if not nums:
94101
return 0
@@ -103,13 +110,11 @@ def find_maximum_xor(self, nums: list[int]) -> int:
103110
doctest.testmod()
104111
print("All doctests passed!")
105112

106-
# Optional: full test suite
107-
print("************ Testing Bitwise Trie Maximum XOR Algorithm ************\n")
113+
# Manual test suite
114+
print("\n************ Manual Testing Bitwise Trie Maximum XOR Algorithm ************\n")
108115
test_cases = [
109116
([3, 10, 5, 25, 2, 8], 28),
110117
([42], 0),
111-
([8, 1], 9),
112-
([0, 0, 0], 0),
113118
([0xFFFFFFFF, 0], 0xFFFFFFFF),
114119
([7, 7, 7], 0),
115120
([1, 2, 3, 4, 5], 7),
@@ -124,5 +129,5 @@ def find_maximum_xor(self, nums: list[int]) -> int:
124129
print(f"Testcase {idx}: Expected={expected}, Got={result}")
125130
assert result == expected, f"Testcase {idx} failed!"
126131

127-
print("\nAll test cases successfully passed!")
132+
print("\nAll manual test cases successfully passed!")
128133
print("********** End of Testing Bitwise Trie Maximum XOR Algorithm **********")

0 commit comments

Comments
 (0)