SHARE
TWEET

Untitled

a guest Jun 18th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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:      # plase 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))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top