From 56b1636eb63394c591a47e80a5f750bc9e22d5ab Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 8 Oct 2025 04:31:36 -0600 Subject: [PATCH] adding update --- .../summary_ranges.py | 29 +++++++++++++++++++ .../test_summary_ranges_round_20.py | 26 +++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_20/summary_ranges.py create mode 100644 tests/test_150_questions_round_20/test_summary_ranges_round_20.py diff --git a/src/my_project/interviews/top_150_questions_round_20/summary_ranges.py b/src/my_project/interviews/top_150_questions_round_20/summary_ranges.py new file mode 100644 index 00000000..8cb378c1 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_20/summary_ranges.py @@ -0,0 +1,29 @@ +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/tests/test_150_questions_round_20/test_summary_ranges_round_20.py b/tests/test_150_questions_round_20/test_summary_ranges_round_20.py new file mode 100644 index 00000000..141d6a71 --- /dev/null +++ b/tests/test_150_questions_round_20/test_summary_ranges_round_20.py @@ -0,0 +1,26 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_20\ +.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]) + + \ No newline at end of file