Last active
October 28, 2019 07:50
-
-
Save Crocmagnon/c4fcbb551dcbd72eaae87a33c334a204 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another option would be to iterate from
firsttolastand check the presence of each integer in the submitted list. I just found the set approach more elegant :)