Start learning free

LESSON 242 OF 673 · HELPER FUNCTIONS

Clamp All in Python: A Helper Method and a Loop

Practice Read the full explanation

You get a list of integers nums and two limits, low and high. Return every number moved inside the limits, in the same order.

A number below low becomes low. A number above high becomes high. Any other number stays as it is.

The work is split between two functions. The first one is a helper. It gets one number and the two limits. It returns that number moved inside them. The second one is the function that is graded. It calls the helper on every number.

Don't use a built-in min, max or clamp function. The helper does the comparing.

 

Example 1:

Input: nums = [12, -3, 7, 25], low = 0, high = 20
Output: [12, 0, 7, 20]
Explanation: -3 is below 0, so it becomes 0.
25 is above 20, so it becomes 20.
12 and 7 are already inside the limits.

Example 2:

Input: nums = [-8, -1, 4], low = -5, high = 5
Output: [-5, -1, 4]
Explanation: The limits can be negative too.
-8 is below -5, so it becomes -5.
-1 and 4 stay as they are.

Example 3:

Input: nums = [3, 9, 6], low = 6, high = 6
Output: [6, 6, 6]
Explanation: Both limits are 6, so every number becomes 6.

 

Constraints:

  • nums has at least one number.
  • Every number is between -1000 and 1000.
  • low and high are between -1000 and 1000.
  • low is never greater than high.
STEP 1 OF 7
    Ctrl + Enter to check

    PYTHON · HELPER FUNCTIONS

    Clamp All in Python, explained

    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

    1. In clamp: if x < low, return low.
    2. Then if x > high, return high.
    3. Otherwise return x. Only a number between the limits gets this far, so it needs no else.
    4. In clampAll: start with an empty list clamped.
    5. Loop with for num in nums.
    6. Inside the loop, append self.clamp(num, low, high).
    7. 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 just clamp(...) inside the method looks for a plain function with that name and fails with a NameError.
    • A return ends the function at once. That is why the two if statements need no elif or else: after return low nothing below runs.
    • When low equals high, every number ends up equal to that value: a smaller number returns low, a larger one returns high, and the one number equal to both stays, as in the third example.
    • The new list clamped leaves nums unchanged.

    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

    numslowhighresult
    [12, -3, 7, 25]020[12, 0, 7, 20]
    [-8, -1, 4]-55[-5, -1, 4]
    [3, 9, 6]66[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 return None.
    • Mixing up the limits. Below low goes to low, above high goes to high. Swapping them sends numbers the wrong way.
    • Changing nums in place. The task asks for a returned list; building a new one is the simplest way.
    Keep going with Python500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free