Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing number algorithm #9203

Merged
Merged
Changes from 2 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
21 changes: 21 additions & 0 deletions bit_manipulation/missing_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def find_missing_number(nums):
"""
Finds the missing number in a list of consecutive integers.

Args:
nums (List[int]): A list of integers.

Returns:
int: The missing number.
Copy link
Member

@cclauss cclauss Oct 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mypy can test data types in the function signature, but not in the comments.

Do not repeat the datatypes in both places because readers will be confused if one is changed and the other is not.

Suggested change
def find_missing_number(nums):
"""
Finds the missing number in a list of consecutive integers.
Args:
nums (List[int]): A list of integers.
Returns:
int: The missing number.
def find_missing_number(nums: list[int]) -> int:
"""
Finds the missing number in a list of consecutive integers.
Args:
nums: A list of integers.
Returns:
The missing number.


Example:
>>> find_missing_number([0, 1, 3, 4])
2
"""
n = len(nums)
missing_number = n

for i in range(n):
missing_number ^= i ^ nums[i]

return missing_number