AlexG2230954

ДЗ. Задание 11

May 22nd, 2022 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. def get_intervals(numbers):
  2.     intervals = []
  3.     current = [numbers[0], numbers[0]]
  4.  
  5.     for number in numbers[1:]:
  6.         if number - current[1] != 1:
  7.             intervals.append(current)
  8.             current = [number, number]
  9.  
  10.         current[1] = number
  11.  
  12.     intervals.append(current)
  13.     return intervals
  14.  
  15.  
  16. def to_str(interval):
  17.     a, b = interval
  18.  
  19.     if b - a == 1: return "{},{}".format(a, b)
  20.     if b - a > 1: return "{}-{}".format(a, b)
  21.     return str(a)
  22.  
  23.  
  24. numbers = list(map(int, input().split()))
  25. intervals = get_intervals(numbers)
  26. print(*map(to_str, intervals), sep=",")
Add Comment
Please, Sign In to add comment