Problem:
Write a Python function, clip(lo, x, hi) that returns lo if x is less than lo; hi if x is greater than hi; and x otherwise. For this problem, y
Here is a solution, assuming that lo < hi.
def clip(lo, x, hi):
return max(lo, min(hi, x))
How it works in each case:
min(hi, x)
returns x
and max(lo, x)
returns lo
.min(hi, x)
returns hi
and if lo < hi, max(lo, hi)
returns hi
min(hi, x)
returns x
and max(lo, x)
returns x