Last active
September 22, 2020 01:55
-
-
Save willy-r/703ba5358751322d4bcc246de710c576 to your computer and use it in GitHub Desktop.
Given a list of ordered integers with some of the numbers missing (and with possible duplicates), find the missing numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def missing_ints(numbers): | |
"""Given a list of ordered integers with some of the | |
numbers missing (and with possible duplicates), find | |
the missing numbers. | |
""" | |
if not numbers: | |
return [] | |
missing_numbers = [n for n in range(min(numbers), max(numbers)) | |
if n not in numbers] | |
return missing_numbers | |
if __name__ == '__main__': | |
CASES = ( | |
([1, 3, 3, 3, 5], [2, 4]), | |
([1, 2, 3, 4, 4, 7, 7], [5, 6]), | |
([8, 8, 10, 12, 13], [9, 11]), | |
([-5, -1, 2, 4], [-4, -3, -2, 0, 1, 3]), | |
([], []), | |
) | |
for list_numbers, expected in CASES: | |
result = missing_ints(list_numbers) | |
assert result == expected, f'{result} != {expected}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment