Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. def foo(l):
  2.     if len(l) == 0:
  3.         return None
  4.  
  5.     if len(l) == 1:
  6.         return str(l[0])
  7.  
  8.     l = sorted(l)
  9.  
  10.     res = ''
  11.     asc = False # ascending
  12.  
  13.     for i in range(1, len(l)):
  14.         if l[i] == l[i-1]+1: # if ascending
  15.             if not asc:      # place first and skip ascending elements
  16.                 asc = True
  17.                 res += str(l[i-1])+'-'
  18.         else: # if next is not ascending,
  19.             res += str(l[i-1])+','
  20.             asc = False
  21.  
  22.     res += str(l[i]) # append last digit
  23.    
  24.     return res
  25.  
  26. if __name__ == "__main__":
  27.     l = [10, 7, 8, 9, 1, 2, 3, 5, 99]
  28.     print(foo(l))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement