Created
January 29, 2023 18:30
-
-
Save les-peters/32fdcacf159af268ce2606eb4dbdc5ef to your computer and use it in GitHub Desktop.
Fill in the Gaps
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
question = """ | |
You are given a list of positive integers which represents some range of | |
integers which has been truncated. Find the missing bits, insert ellipses | |
to show that that part has been truncated, and print it. | |
If the consecutive values differ by exactly two, then insert | |
the missing value. | |
Examples: | |
> missingBits([1,2,3,4,20,21,22,23]) | |
> "[1,2,3,4,...,20,21,22,23]" | |
> missingBits([1,2,3,5,6]) | |
> "[1,2,3,4,5,6]" | |
> missingBits([1,3,20,27]) | |
> "[1,2,3,...,20,...,27]" | |
""" | |
def missingBits(arr): | |
filled_arr = [] | |
for i in range(0,len(arr)-1): | |
filled_arr.append(str(arr[i])) | |
if arr[i+1] == arr[i] + 2: | |
filled_arr.append(str(arr[i]+1)) | |
elif arr[i+1] > arr[i] + 1: | |
filled_arr.append('...') | |
filled_arr.append(str(arr[-1])) | |
return ','.join(filled_arr) | |
print(missingBits([1,2,3,4,20,21,22,23])) | |
print(missingBits([1,2,3,5,6])) | |
print(missingBits([1,3,20,27])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment