387 B
387 B
title, tags
| title | tags |
|---|---|
| clamp_number | math,beginner |
Clamps num within the inclusive range specified by the boundary values a and b.
- If
numfalls within the range, returnnum. - Otherwise, return the nearest number in the range.
def clamp_number(num,a,b):
return max(min(num, max(a, b)), min(a, b))
clamp_number(2, 3, 5) # 3
clamp_number(1, -1, -5) # -1