fork(2) download
  1. input = [x for x in range(5, 51)] + [x for x in range(130, 151)] + [200, 201, 220]
  2.  
  3. beginning, current = 0, 0
  4. output = []
  5. for number in sorted(input):
  6. if not beginning:
  7. beginning = number
  8. current = number
  9. continue
  10. if number == (current + 1):
  11. current = number
  12. else:
  13. if current == beginning:
  14. output.append((current, ))
  15. else:
  16. output.append((beginning, current))
  17. beginning = number
  18. current = number
  19. if current == beginning:
  20. output.append((current, ))
  21. else:
  22. output.append((beginning, current))
  23.  
  24. print(input, ' => ', output)
  25.  
Success #stdin #stdout 0.02s 8736KB
stdin
Standard input is empty
stdout
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 200, 201, 220]  =>  [(5, 50), (130, 150), (200, 201), (220,)]