Created
October 28, 2019 05:48
-
-
Save arkadyark/0cfb4c7da582f3e25acc231d420d33bf to your computer and use it in GitHub Desktop.
Missing Integers interview question
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
''' | |
Given a list of ordered integers with some of the numbers missing (and with possible duplicates), find the missing numbers. | |
Example: | |
> missingInts([1, 3, 3, 3, 5, 6]) | |
> 2, 4 | |
> missingInts([1, 2, 3, 4, 4, 7, 7]) | |
> 5, 6 | |
''' | |
def missingInts(l): | |
missing = [] | |
prev = l[0] | |
for i in range(1, len(l)): | |
if l[i] == prev: | |
# Duplicates, just keep moving | |
continue | |
elif l[i] == prev - 1: | |
prev = l[i] | |
else: | |
missing += list(range(prev+1, l[i])) | |
prev = l[i] | |
return missing | |
if __name__ == '__main__': | |
print(missingInts([1, 3, 3, 3, 5, 6])) | |
print(missingInts([1, 2, 3, 4, 4, 7, 7])) | |
print(missingInts([2, 4, 7, 7])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment