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

前端 未结 9 1765
滥情空心
滥情空心 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条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-10 11:36

    Another solution:

    def clip(lo, x, hi):
        result = {x: x}
        result[min(x, lo)] = lo
        result[max(x, hi)] = hi
        return result[x]
    

提交回复
热议问题