Problem overview
The method clampAll receives a list of integers nums and two limits, low and high, with low never greater than high. It returns a list of the same length and order in which every number has been moved inside the limits. A number below low becomes low, a number above high becomes high, and any other number stays as it is.
For nums = [12, -3, 7, 25], low = 0 and high = 20 the answer is [12, 0, 7, 20]. With nums = [-8, -1, 4], low = -5 and high = 5 it is [-5, -1, 4]. With nums = [3, 9, 6] and both limits equal to 6, every number becomes 6: [6, 6, 6].
Approach: split the work
The task is built on a helper: a small function that does one job, so the bigger function can stay simple. Here the helper, clamp, handles one number: it receives x, low and high and returns x moved inside the limits. clampAll then only has to call it once for each number and collect the answers.
This keeps each piece easy to read. The helper contains all the comparing, and the loop in clampAll contains none. If the rule ever changed, only the helper would change.
Step by step
- In
clamp: ifx < low, returnlow. - Then if
x > high, returnhigh. - Otherwise return
x. Only a number between the limits gets this far, so it needs noelse. - In
clampAll: start with an empty listclamped. - Loop with
for num in nums. - Inside the loop, append
self.clamp(num, low, high). - After the loop, return
clamped.
The solution in Python
class Solution:
def clamp(self, x: int, low: int, high: int) -> int:
if x < low:
return low
if x > high:
return high
return x
def clampAll(self, nums: List[int], low: int, high: int) -> List[int]:
clamped = []
for num in nums:
clamped.append(self.clamp(num, low, high))
return clamped
Details to notice:
- Both functions are methods of the class, so one calls the other as
self.clamp(...). Writing justclamp(...)inside the method looks for a plain function with that name and fails with aNameError. - A
returnends the function at once. That is why the twoifstatements need noeliforelse: afterreturn lownothing below runs. - When
lowequalshigh, every number ends up equal to that value: a smaller number returnslow, a larger one returnshigh, and the one number equal to both stays, as in the third example. - The new list
clampedleavesnumsunchanged.
Why return, not print
The helper hands its answer back with return, so the caller can use the value: clampAll appends it to a list. A helper that only printed the number would show it on the screen, but clampAll would receive None and the list would fill with None values. Helpers almost always return their result and leave printing to the code that calls them.
Tracing the first example
With low = 0 and high = 20, the loop makes four calls. clamp(12, 0, 20) fails both tests and returns 12. clamp(-3, 0, 20) passes the first test and returns 0. clamp(7, 0, 20) returns 7. clamp(25, 0, 20) fails the first test, passes the second and returns 20. The list built from those answers, in order, is [12, 0, 7, 20].
Testing
| nums | low | high | result |
|---|---|---|---|
[12, -3, 7, 25] | 0 | 20 | [12, 0, 7, 20] |
[-8, -1, 4] | -5 | 5 | [-5, -1, 4] |
[3, 9, 6] | 6 | 6 | [6, 6, 6] |
Common mistakes
- Returning inside the loop. The method would hand back a single number instead of the whole list.
- Forgetting the final
return x. A number inside the limits would make the helper returnNone. - Mixing up the limits. Below
lowgoes tolow, abovehighgoes tohigh. Swapping them sends numbers the wrong way. - Changing
numsin place. The task asks for a returned list; building a new one is the simplest way.
