Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def foo(l):
- if len(l) == 0:
- return None
- if len(l) == 1:
- return str(l[0])
- l = sorted(l)
- res = ''
- asc = False # ascending
- for i in range(1, len(l)):
- if l[i] == l[i-1]+1: # if ascending
- if not asc: # place first and skip ascending elements
- asc = True
- res += str(l[i-1])+'-'
- else: # if next is not ascending,
- res += str(l[i-1])+','
- asc = False
- res += str(l[i]) # append last digit
- return res
- if __name__ == "__main__":
- l = [10, 7, 8, 9, 1, 2, 3, 5, 99]
- print(foo(l))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement