-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
Sep 17, 2024 | ||
|
||
* solve "228. Summary Ranges" | ||
|
||
Sep 16, 2024 | ||
|
||
* re-solve question 28 using Knuth–Morris–Pratt | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution: | ||
def summaryRanges(self, nums: list[int]) -> list[str]: | ||
if not nums: | ||
return [] | ||
|
||
result: list[str] = [] | ||
left = right = nums[0] | ||
|
||
def append() -> None: | ||
if left == right: | ||
result.append(f"{left}") | ||
else: | ||
result.append(f"{left}->{right}") | ||
|
||
for i in range(1, len(nums)): | ||
if nums[i] == right + 1: | ||
right = nums[i] | ||
else: | ||
append() | ||
left = right = nums[i] | ||
|
||
append() | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pytest | ||
|
||
from src.summary_ranges import Solution | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"expected,nums", | ||
( | ||
(["0->2", "4->5", "7"], [0, 1, 2, 4, 5, 7]), | ||
(["0", "2->4", "6", "8->9"], [0, 2, 3, 4, 6, 8, 9]), | ||
), | ||
) | ||
def test_solution(expected, nums): | ||
assert expected == Solution().summaryRanges(nums) |