总结:
- Q5:非纯函数副作用;with_if_statement()和with_if_function()区别。
- Q6:用 // 去掉小数,print(n)和count+=1与if平级。
Q2: a_plus_abs_b
from operator import add, mul
def a_plus_abs_b(a, b):
"""Retrun a+abs(b), but without calling abs.
>>> a_plus_abs_b(2, -3)
5
>>> a_plus_abs_b(2, 3)
5
"""
if b < 0:
return add(a, -b)
else:
return add(a, b)
Q3: two_of_three
def two_of_three(x,y,z):
"""Return a*a + b*b, when a and b are the two smallest members of the positive numbers x, y, and z.
>>> two_of_three(1, 2, 3)
5
>>> two_of_three(3, 4, 5)
25
>>> two_of_three(5, 5, 5)
50
"""
return x**2 + y**2 +z**2 - max(x,y,z)**2