Skip to content

Commit fb8108f

Browse files
authored
Merge pull request #1136 from ivan1016017/december29
summary ranges
2 parents cc37418 + 6e6a7f8 commit fb8108f

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def summaryRanges(self, nums: List[int]) -> List[str]:
6+
7+
len_nums = len(nums)
8+
9+
if len_nums == 0:
10+
return []
11+
elif len_nums == 1:
12+
return [f'{nums[0]}']
13+
else:
14+
answer = list()
15+
pre = start = nums[0]
16+
17+
for i in nums[1:]:
18+
if i - pre > 1:
19+
answer.append(f'{start}->{pre}' if pre-start > 0 else
20+
f'{start}')
21+
start = i
22+
pre = i
23+
24+
answer.append(f'{start}->{pre}' if pre-start > 0 else f'{start}')
25+
26+
return answer
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_12\
3+
.summary_ranges import Solution
4+
5+
6+
class SummaryRangesTestCase(unittest.TestCase):
7+
8+
def test_empty(self):
9+
solution = Solution()
10+
output = solution.summaryRanges(nums=[])
11+
target = []
12+
self.assertEqual(target, output)
13+
14+
def test_single_element(self):
15+
solution = Solution()
16+
output = solution.summaryRanges(nums=[1])
17+
target = ['1']
18+
self.assertEqual(target, output)
19+
20+
def test_several_elements(self):
21+
solution = Solution()
22+
output = solution.summaryRanges(nums=[0,1,2,4,5,7])
23+
target = ["0->2","4->5","7"]
24+
for k, v in enumerate(target):
25+
self.assertEqual(v, output[k])

0 commit comments

Comments
 (0)