How to apply max & min boundaries to a value without using conditional statements

前端 未结 9 1794
滥情空心
滥情空心 2021-02-10 11:01

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

9条回答
  •  Happy的楠姐
    2021-02-10 11:32

    Without giving out the whole solution - you don't need to "check" anything. A value limited to lo from the bottom is what you get from running max(x, lo).

    Also value clipped to one boundary is not going to be affected by clipping to the other boundary, so you can safely run the result of one correction through another one.

提交回复
热议问题