Skip to content

Commit d09f855

Browse files
authored
Merge pull request #1515 from ivan1016017/january13
adding algo
2 parents ffdbc64 + 0c69969 commit d09f855

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
15+
answer = list()
16+
17+
pre = start = nums[0]
18+
19+
for i in nums[1:]:
20+
21+
if i - pre > 1:
22+
answer.append(f'{start}->{pre}' if pre - start > 0 else
23+
f'{start}')
24+
start = i
25+
26+
pre = i
27+
28+
answer.append(f'{start}->{pre}' if pre - start > 0 else f'{start}')
29+
30+
return answer
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
6+
7+
list_answer = list()
8+
9+
for i in sorted(intervals, key=lambda x: x[0]):
10+
11+
if list_answer and i[0] <= list_answer[-1][-1]:
12+
list_answer[-1][-1] = max(list_answer[-1][-1], i[-1])
13+
else:
14+
list_answer.append(i)
15+
16+
return list_answer
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_46_summary_ranges import Solution
4+
5+
class SummaryRangesTestCase(unittest.TestCase):
6+
7+
def test_empty(self):
8+
solution = Solution()
9+
output = solution.summaryRanges(nums=[])
10+
target = []
11+
self.assertEqual(target, output)
12+
13+
def test_single_element(self):
14+
solution = Solution()
15+
output = solution.summaryRanges(nums=[1])
16+
target = ['1']
17+
self.assertEqual(target, output)
18+
19+
def test_several_elements(self):
20+
solution = Solution()
21+
output = solution.summaryRanges(nums=[0,1,2,4,5,7])
22+
target = ["0->2","4->5","7"]
23+
for k, v in enumerate(target):
24+
self.assertEqual(v, output[k])
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_47_merge_intervals import Solution
4+
5+
class MergeIntervalsTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.merge(intervals = [[1,3],[2,6],[8,10],[15,18]])
10+
target = [[1,6],[8,10],[15,18]]
11+
self.assertEqual(output, target)
12+
13+
def test_second_pattern(self):
14+
solution = Solution()
15+
output = solution.merge(intervals = [[1,4],[4,5]])
16+
target = [[1,5]]
17+
self.assertEqual(output, target)
18+
19+
def test_third_pattern(self):
20+
solution = Solution()
21+
output = solution.merge(intervals = [[4,7],[1,4]])
22+
target = [[1,7]]
23+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)