Editor

Keep It in Range

#510 · Functions · Beginner · 25 XP · about 3 min

Write clamp(value, low=0, high=100) that keeps value inside the range from low to high:

  • a value below low becomes low,
  • a value above high becomes high,
  • a value inside the range stays as it is.

When low and high are left out, the range is 0 to 100, like a volume slider.

Examples

clamp(150)          # returns 100
clamp(-20)          # returns 0
clamp(42)           # returns 42
clamp(7, 1, 6)      # returns 6
clamp(15, high=10)  # returns 10

Note

low is never bigger than high.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.