Files
30-seconds-of-code/snippets/clamp_number.md
Isabelle Viktoria Maciohsek cbc78ee450 Bake dates into snippets
2021-06-13 19:38:10 +03:00

464 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
clamp_number math,beginner 2019-08-20T12:50:38+03:00 2020-11-02T19:27:07+02:00

Clamps num within the inclusive range specified by the boundary values.

  • If num falls within the range (a, b), return num.
  • 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