if/elif/else怎么能换成min()和max()呢?
How can if/elif/else be replaced with min() and max()?
正在做 MIT's OpenCourseWare 6.01SC 的练习。问题 3.1.5:
Define a function clip(lo, x, hi)
that returns lo
if x
is less than lo
, returns hi
if x
is greater than hi
, and returns x
otherwise. You can assume that lo < hi
. ...don't use if
, but use min
and max
.
用英语重新表述,如果 x
是参数中最少的,return lo
;如果 x
是最大的参数,则 return hi
;否则,return x
。因此:
def clip(lo, x, hi):
if min(lo, x, hi) == x:
return lo
elif max(lo, x, hi) == x:
return hi
else:
return x
也许我没有正确理解问题,但我完全不知道如何在不使用 if
的情况下 return 结果。如何修改函数以删除 if/elif/else 语句?
Link to original problem 3.1.5
Link to previous problem 3.1.4 (for context)
编辑:
Comments/answers 这个问题帮助我意识到,我原来的简单英语重新表述并不是思考问题的好方法。考虑它的更好方法是确定哪个论点介于其他两个论点之间。
你可以return这个公式:
x + lo + hi - max(x, lo, hi) - min(x, lo, hi)
案例论证:
案例 1:
If min(lo, x, hi) = lo and max(lo, x, hi) = hi
x + lo + hi - max(x, lo, hi) - min(x, lo, hi) ==> x + lo + hi - hi - lo ==> x
案例二:
If min(lo, x, hi) = lo and max(lo, x, hi) = x
x + lo + hi - max(x, lo, hi) - min(x, lo, hi) ==> x + lo + hi - x - lo ==> hi
案例 3:
If min(lo, x, hi) = x and max(lo, x, hi) = hi
x + lo + hi - max(x, lo, hi) - min(x, lo, hi) ==> x + lo + hi - hi - x ==> lo
公式 return 是所有可能情况下的预期答案。
一行代码:
#! python3.8
def clip(lo, x, hi):
return max(min(x, hi), lo)
print(clip(1, 2, 3))
print(clip(2, 1, 3))
print(clip(1, 3, 2))
# Output
# 2
# 2
# 2
给你,一个完全不使用 if-else 的值检查功能。虽然块只会 运行 一次,所以没有冗余。
def clip(lo, x, hi):
low = (min(lo, x) == x)
high = (max(x, hi) == x)
while low:
return lo
while high:
return hi
return x
编辑:我不知道他为什么否决我的代码
正在做 MIT's OpenCourseWare 6.01SC 的练习。问题 3.1.5:
Define a function
clip(lo, x, hi)
that returnslo
ifx
is less thanlo
, returnshi
ifx
is greater thanhi
, and returnsx
otherwise. You can assume thatlo < hi
. ...don't useif
, but usemin
andmax
.
用英语重新表述,如果 x
是参数中最少的,return lo
;如果 x
是最大的参数,则 return hi
;否则,return x
。因此:
def clip(lo, x, hi):
if min(lo, x, hi) == x:
return lo
elif max(lo, x, hi) == x:
return hi
else:
return x
也许我没有正确理解问题,但我完全不知道如何在不使用 if
的情况下 return 结果。如何修改函数以删除 if/elif/else 语句?
Link to original problem 3.1.5
Link to previous problem 3.1.4 (for context)
编辑:
Comments/answers 这个问题帮助我意识到,我原来的简单英语重新表述并不是思考问题的好方法。考虑它的更好方法是确定哪个论点介于其他两个论点之间。
你可以return这个公式:
x + lo + hi - max(x, lo, hi) - min(x, lo, hi)
案例论证:
案例 1:
If min(lo, x, hi) = lo and max(lo, x, hi) = hi
x + lo + hi - max(x, lo, hi) - min(x, lo, hi) ==> x + lo + hi - hi - lo ==> x
案例二:
If min(lo, x, hi) = lo and max(lo, x, hi) = x
x + lo + hi - max(x, lo, hi) - min(x, lo, hi) ==> x + lo + hi - x - lo ==> hi
案例 3:
If min(lo, x, hi) = x and max(lo, x, hi) = hi
x + lo + hi - max(x, lo, hi) - min(x, lo, hi) ==> x + lo + hi - hi - x ==> lo
公式 return 是所有可能情况下的预期答案。
一行代码:
#! python3.8
def clip(lo, x, hi):
return max(min(x, hi), lo)
print(clip(1, 2, 3))
print(clip(2, 1, 3))
print(clip(1, 3, 2))
# Output
# 2
# 2
# 2
给你,一个完全不使用 if-else 的值检查功能。虽然块只会 运行 一次,所以没有冗余。
def clip(lo, x, hi):
low = (min(lo, x) == x)
high = (max(x, hi) == x)
while low:
return lo
while high:
return hi
return x
编辑:我不知道他为什么否决我的代码