Skip to content

Commit

Permalink
test: adding more tests to missing number algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
kibolho committed Oct 13, 2023
1 parent d96029e commit 1f3b829
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions bit_manipulation/missing_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@ def find_missing_number(nums: list[int]) -> int:
Example:
>>> find_missing_number([0, 1, 3, 4])
2
>>> find_missing_number([4, 3, 1, 0])
2
>>> find_missing_number([-2, 0, 1, 3, 4])
Traceback (most recent call last):
...
ValueError: negative values not supported
"""
n = len(nums)
missing_number = n

for i in range(n):
if nums[i] < 0:
raise ValueError("negative values not supported")
missing_number ^= i ^ nums[i]

return missing_number


if __name__ == "__main__":
import doctest

doctest.testmod()

0 comments on commit 1f3b829

Please sign in to comment.