File tree 1 file changed +13
-15
lines changed
1 file changed +13
-15
lines changed Original file line number Diff line number Diff line change 6
6
"""
7
7
8
8
9
- def summarize_ranges (array ):
10
- """
11
- :type array: List[int]
12
- :rtype: List[]
13
- """
9
+ from typing import List
10
+
11
+ def summarize_ranges (array : List [int ]) -> List [str ]:
14
12
res = []
15
13
if len (array ) == 1 :
16
14
return [str (array [0 ])]
17
- i = 0
18
- while i < len (array ):
19
- num = array [i ]
20
- while i + 1 < len (array ) and array [i + 1 ] - array [i ] == 1 :
21
- i += 1
22
- if array [i ] != num :
23
- res .append ((num , array [i ]))
15
+ it = iter (array )
16
+ start = end = next (it )
17
+ for num in it :
18
+ if num - end == 1 :
19
+ end = num
24
20
else :
25
- res .append ((num , num ))
26
- i += 1
27
- return res
21
+ res .append ((start , end ) if start != end else (start ,))
22
+ start = end = num
23
+ res .append ((start , end ) if start != end else (start ,))
24
+ return [f"{ r [0 ]} -{ r [1 ]} " if len (r ) > 1 else str (r [0 ]) for r in res ]
25
+
You can’t perform that action at this time.
0 commit comments