diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_46_summary_ranges.py b/src/my_project/interviews/top_150_questions_round_22/ex_46_summary_ranges.py new file mode 100644 index 00000000..8d7b9f76 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_46_summary_ranges.py @@ -0,0 +1,30 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def summaryRanges(self, nums: List[int]) -> List[str]: + + len_nums = len(nums) + + if len_nums == 0: + return [] + elif len_nums == 1: + return [f'{nums[0]}'] + else: + + answer = list() + + pre = start = nums[0] + + for i in nums[1:]: + + if i - pre > 1: + answer.append(f'{start}->{pre}' if pre - start > 0 else + f'{start}') + start = i + + pre = i + + answer.append(f'{start}->{pre}' if pre - start > 0 else f'{start}') + + return answer \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_47_merge_intervals.py b/src/my_project/interviews/top_150_questions_round_22/ex_47_merge_intervals.py new file mode 100644 index 00000000..c71a5069 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_47_merge_intervals.py @@ -0,0 +1,16 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def merge(self, intervals: List[List[int]]) -> List[List[int]]: + + list_answer = list() + + for i in sorted(intervals, key=lambda x: x[0]): + + if list_answer and i[0] <= list_answer[-1][-1]: + list_answer[-1][-1] = max(list_answer[-1][-1], i[-1]) + else: + list_answer.append(i) + + return list_answer \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_46_summary_ranges_round_22.py b/tests/test_150_questions_round_22/test_46_summary_ranges_round_22.py new file mode 100644 index 00000000..22937346 --- /dev/null +++ b/tests/test_150_questions_round_22/test_46_summary_ranges_round_22.py @@ -0,0 +1,24 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_46_summary_ranges import Solution + +class SummaryRangesTestCase(unittest.TestCase): + + def test_empty(self): + solution = Solution() + output = solution.summaryRanges(nums=[]) + target = [] + self.assertEqual(target, output) + + def test_single_element(self): + solution = Solution() + output = solution.summaryRanges(nums=[1]) + target = ['1'] + self.assertEqual(target, output) + + def test_several_elements(self): + solution = Solution() + output = solution.summaryRanges(nums=[0,1,2,4,5,7]) + target = ["0->2","4->5","7"] + for k, v in enumerate(target): + self.assertEqual(v, output[k]) diff --git a/tests/test_150_questions_round_22/test_47_merge_intervals_round_22.py b/tests/test_150_questions_round_22/test_47_merge_intervals_round_22.py new file mode 100644 index 00000000..0a303306 --- /dev/null +++ b/tests/test_150_questions_round_22/test_47_merge_intervals_round_22.py @@ -0,0 +1,23 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_47_merge_intervals import Solution + +class MergeIntervalsTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.merge(intervals = [[1,3],[2,6],[8,10],[15,18]]) + target = [[1,6],[8,10],[15,18]] + self.assertEqual(output, target) + + def test_second_pattern(self): + solution = Solution() + output = solution.merge(intervals = [[1,4],[4,5]]) + target = [[1,5]] + self.assertEqual(output, target) + + def test_third_pattern(self): + solution = Solution() + output = solution.merge(intervals = [[4,7],[1,4]]) + target = [[1,7]] + self.assertEqual(output, target) \ No newline at end of file