Last active
July 14, 2020 13:49
-
-
Save ruan65/9333c0f4823fbb35b6d62a4b7ddcc57c 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
def get_str_of_intervals(arr: []) -> str: | |
if not arr: | |
return '' | |
breaks = [] | |
arr.sort() | |
start = end = arr[0] | |
for i in arr[1:]: | |
diff = i - end | |
if diff == 1: | |
end = i | |
else: | |
breaks.append((start, end)) | |
start = end = i | |
breaks.append((start, end)) | |
return ','.join(str(s) if s == e else f'{s}-{e}' for s, e in breaks) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment