Keep Inside Range

Apps often need to keep a value within allowed limits, like making sure volume does not go below the minimum or above the maximum. The function clamp receives a number n and limits it to the range from lo to hi. If n is below the range, return lo; if it is above the range, return hi; otherwise return n itself.

Примеры

Ввод
(5, 0, 10)
Вывод
5
Ввод
(-3, 0, 10)
Вывод
0
Ввод
(15, 0, 10)
Вывод
10

Нужно реализовать

def clamp(n: int, lo: int, hi: int) -> int:

Теги

math
PostgreSQLv16

Koodin suorituksen tulos näkyy tässä

Visible tests
1
Input
5, 0, 10
Expected5
2
Input
-3, 0, 10
Expected0
3
Input
15, 0, 10
Expected10
4
Input
0, 0, 10
Expected0
5
Input
10, 0, 10
Expected10
6
Input
10, 999999, 10
Expected999999
Focus radio
Paused · SomaFM · Fluid