Back to Practice
#0152

Clamp Values to a Range

EasyNumPy15 min10 XP

Problem

Given an array and low/high bounds, return a new array where every value is between low and high.

Why This Matters

Clipping is common for image pixels, robust preprocessing, and bounded model outputs.

Function Signature

def clamp_values(arr, low, high):

Examples

Example 1
Inputnp.array([-2, 0, 5, 12]), low = 0, high = 10
Outputnp.array([0, 0, 5, 10])

Values below 0 become 0; values above 10 become 10.

Constraints

  • Return the exact requested value.
  • Handle empty or small inputs when the prompt allows them.
  • Keep the code readable and deterministic.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputnp.array([-2, 0, 5, 12]), low = 0, high = 10
Expectednp.array([0, 0, 5, 10])

Values below 0 become 0; values above 10 become 10.

Hidden Test Categories
empty or smallest inputduplicates or repeated valueslarger realistic input