SHARE
TWEET

Untitled

a guest Jul 12th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #целые числа без повторов. Надо преобразовать в строку.
  2. # [1,4,5,2,3,9,8,11,0] => "0-5,8-9,11"
  3.  
  4. input = [0, 2, 4, 5, 6, 8, 9, 11, 12, 14, 16, 17, 18, 19, 24, 25, 28, 29]
  5.  
  6. x = str(input[0])
  7. counter = -1
  8. prev = 45675765
  9. next = 69696969
  10.  
  11. last_index = len(input) - 1
  12. last_element = input[last_index]
  13.  
  14. for element in input:
  15.     counter = counter + 1
  16.     prev = counter - 1
  17.     next = counter + 1
  18.    
  19.     if element == input[0]:
  20.         continue
  21.      
  22.     if element == last_element and element - 1 == input[prev]:
  23.         x = x + '-' + str(element)
  24.         continue
  25.     if element == last_element and element - 1 != input[prev]:  
  26.         x = x + ',' + str(element)
  27.         continue
  28.  
  29.     if element - 1 == input[prev] and element + 1 != input[next]:
  30.         x = x + '-' + str(element)
  31.        
  32.     if element - 1 == input[prev]:
  33.         continue
  34.        
  35.     else:
  36.         x = x + ',' + str(element)
  37.  
  38. print(x)
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