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
Melde dich an, um den Einreichungsverlauf zu sehen
AnmeldenMelde dich an, um den KI-Mentor zu nutzen
AnmeldenVisible tests
1
Input
5, 0, 10Expected
52
Input
-3, 0, 10Expected
03
Input
15, 0, 10Expected
104
Input
0, 0, 10Expected
05
Input
10, 0, 10Expected
106
Input
10, 999999, 10Expected
999999Focus radio
Paused · SomaFM · Fluid