From 2f7fca38730a636b5e0b9b0a655ade9f6af71a54 Mon Sep 17 00:00:00 2001 From: John Polychronopoulos <46429733+JohnnyPol@users.noreply.github.com> Date: Tue, 4 Mar 2025 21:14:35 +0200 Subject: [PATCH] Refactor Summary Ranges - Leetcode 228.py: Use f-strings and simplify logic f-strings make the code more readable and stored the len(nums) to avoid constant len() calls. --- .../Summary Ranges - Leetcode 228.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Summary Ranges - Leetcode 228/Summary Ranges - Leetcode 228.py b/Summary Ranges - Leetcode 228/Summary Ranges - Leetcode 228.py index 25cb1e1..3ce4924 100644 --- a/Summary Ranges - Leetcode 228/Summary Ranges - Leetcode 228.py +++ b/Summary Ranges - Leetcode 228/Summary Ranges - Leetcode 228.py @@ -2,16 +2,13 @@ class Solution: def summaryRanges(self, nums: List[int]) -> List[str]: ans = [] i = 0 - - while i < len(nums): + n = len(nums) + while i < n: start = nums[i] - while i < len(nums)-1 and nums[i] + 1 == nums[i + 1]: + while i < n - 1 and nums[i] + 1 == nums[i + 1]: i += 1 - if start != nums[i]: - ans.append(str(start) + "->" + str(nums[i])) - else: - ans.append(str(nums[i])) + ans.append(f"{start}->{nums[i]}" if start != nums[i] else f"{nums[i]}") i += 1