Add clamp_number snippet

This commit is contained in:
Angelos Chalaris
2019-08-20 12:50:38 +03:00
parent dfcd48ed17
commit 613a56cc86

19
snippets/clamp_number.md Normal file
View File

@ -0,0 +1,19 @@
---
title: clamp_number
tags: math,beginner
---
Clamps `num` within the inclusive range specified by the boundary values `a` and `b`.
If `num` falls within the range, return `num`.
Otherwise, return the nearest number in the range.
```py
def clamp_number(num,a,b):
return max(min(num, max(a,b)),min(a,b))
```
```py
clamp_number(2, 3, 5) # 3
clamp_number(1, -1, -5) # -1
```