比如[1,2,4,6],最大6,最小1,返回6-1=5
我写的代码:
def checkio(*args):
if not args:
return 0
return max(args)-min(args)
另外两个写得更简洁的代码:
版本A:
def checkio(*args):
return max(args) -min(args) if args else 0
版本B:
def checkio(*t):
return len(t) and max(t)-min(t)
版本A中if else为什么不用分号?
版本B我不明白为什么这么写能实现和我一样的功能。len(t)是一个数值,max(t)-min(t)是一个数值,两个数值and一下,怎么就能实现这个功能了呢?谢谢!
当输入为空的list的时候返回0。
catspeake