Fix the clamp helper for a list of values

Review the AI's codeEasy10 min

Your task

An AI wrote a clamp helper that keeps every value inside a range. It is short enough to look obviously right, which is exactly when a reading misses the forgotten case. Your team checks it against a written contract instead.

An assistant wrote clamp_values(values, low, high). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • values: a list of integers.
  • low, high: the two bounds of the range, as integers.
  • Return a new list with each value clamped, in the same order.

Rules the tests check:

  • If low is greater than high, the bounds came in the wrong order: swap them, so the range runs from high to low.
  • The range includes both bounds. A value inside it, or equal to a bound, stays as it is.
  • A value below the range becomes the lower bound. A value above it becomes the upper bound.
  • An empty list returns an empty list.

Examples

clamp_values(values: list[int], low: int, high: int) -> list[int]
Example 1
Inputclamp_values([5, -3, 12], 0, 10)
Output[5, 0, 10]
Example 2
Inputclamp_values([0, 10], 0, 10)
Output[0, 10]
Example 3
Inputclamp_values([7, -2, 99], 10, 0)
Output[7, 0, 10]