1
2
3
4
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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This script takes a list of numbers from command line and prints them in the compact form.
Example:
Input: 1,2,3,4,9,10,14,15,16
Output: 1-4,9,10,14-16
"""
import sys
def CompactList(numbers):
"""This function takes a list of numbers as input and returns the list in compact form.
Args:
numbers (list): A list of numbers
Returns:
str: The list in compact form
Examples:
>>> CompactList([1,2,3,4,9,10,14,15,16])
'1-4,9,10,14-16'
"""
compact_list = []
start = numbers[0]
end = numbers[0]
for i in range(1, len(numbers)):
if numbers[i] - numbers[i - 1] == 1:
end = numbers[i]
else:
if start == end:
compact_list.append(str(start))
else:
compact_list.append(str(start) + "-" + str(end))
start = numbers[i]
end = numbers[i]
if start == end:
compact_list.append(str(start))
else:
compact_list.append(str(start) + "-" + str(end))
return ",".join(compact_list)
if __name__ == "__main__":
numbers = [int(i) for i in sys.argv[1].split(",")]
print(CompactList(numbers))
|