Skip to content

Instantly share code, notes, and snippets.

@Crocmagnon
Last active October 28, 2019 07:50
Show Gist options
  • Select an option

  • Save Crocmagnon/c4fcbb551dcbd72eaae87a33c334a204 to your computer and use it in GitHub Desktop.

Select an option

Save Crocmagnon/c4fcbb551dcbd72eaae87a33c334a204 to your computer and use it in GitHub Desktop.
from typing import List, Set
def missing_ints(numbers: List[int]) -> Set[int]:
if not numbers:
return set()
first = numbers[0]
last = numbers[-1]
whole_range = set(range(first, last + 1))
existing = set(numbers)
return whole_range - existing
def main():
assert missing_ints([]) == set(), "Empty list"
assert missing_ints([-3, 2]) == {-2, -1, 0, 1}, "Lower and upper limits"
assert missing_ints([-5, -2, -1, 3, 6]) == {-4, -3, 0, 1, 2, 4, 5}, "Negative numbers"
assert missing_ints([1, 2, 3, 4, 5]) == set(), "No missing"
assert missing_ints([1, 3, 3, 3, 5, 6]) == {2, 4}, "Duplicates in the middle of the list"
assert missing_ints([1, 2, 3, 4, 7, 7]) == {5, 6}, "Duplicates at the end of the list"
assert missing_ints([1, 1, 2, 3, 5, 7]) == {4, 6}, "Duplicates at the beginning of the list"
if __name__ == '__main__':
main()
@Crocmagnon
Copy link
Author

Another option would be to iterate from first to last and check the presence of each integer in the submitted list. I just found the set approach more elegant :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment