Summary
- 函数调用
- 非纯函数
- //运算符,结果为整数
Q2: A Plus Abs B
from operator import add, sub
def a_plus_abs_b(a, b):
"""Return a+abs(b), but without calling abs.
>>> a_plus_abs_b(2, 3)
5
>>> a_plus_abs_b(2, -3)
5
>>> # a check that you didn't change the return statement!
>>> import inspect, re
>>> re.findall(r'^\s*(return .*)', inspect.getsource(a_plus_abs_b), re.M)
['return f(a, b)']
"""
if b < 0:
f = sub
else:
f = add
return f(a, b)
- return表明f为一个函数
Q3: Two of Three
def two_of_three(x, y, z):
"""Return a*a + b*b, where 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(5, 3, 1)
10
>>> two_of_three(10, 2, 8)
68
>>> two_of_three(5, 5, 5)
50
>>> # check that your code consists of nothing but an expression (this docstring)
>>> # a return statement
>>> import inspect, ast
>>> [type(x).__name__ for x in ast.parse(inspect.getsource(two_of_three)).body[0].body]
['Expr', 'Return']
"""
return x**2+y**2+z**2-max(x,y,z)**2
Q4: Largest Factor
我的解答:
def largest_factor(n):
"""Return the largest factor of n that is smaller than n.
>>> largest_factor(15) # factors are 1, 3, 5
5
>>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40
40
>>> largest_factor(13) # factor is 1 since 13 is prime
1
"""
list = []
k=1
while k<n:
if n%k == 0:
list.append(k)
k += 1
return list[-1]
参考作业提示视频后:
k = n-1 #要找到比n小的最大除数→从n-1开始
while n%k != 0:
k -= 1 #对于每个比n小的数,看是否能被整除
return k
改进
half_n = n // 2
largestfactor = half_n
while n % largestfactor != 0:
largestfactor = largestfactor -1
return largestfactor
参考答案
factor = n - 1
while factor > 0:
if n % factor == 0:
return factor
factor -= 1
Q5:If Function vs Statement
def if_function(condition, true_result, false_result):
"""Return true_result if condition is a true value, and
false_result otherwise.
>>> if_function(True, 2, 3)
2
>>> if_function(False, 2, 3)
3
>>> if_function(3==2, 3+2, 3-2)
1
>>> if_function(3>2, 3+2, 3-2)
5
"""
if condition:
return true_result
else:
return false_result
def with_if_statement():
"""
>>> result = with_if_statement()
47
>>> print(result)
None
"""
if cond(): # 调用cond()函数
return true_func() #如果条件为真,则只会调用true_func(),false_func()将永远不会被调用
else:
return false_func() #如果条件为假,则只会调用false_func(),true_func()将永远不会被调用
def with_if_function():
"""
>>> result = with_if_function()
42
47
>>> print(result)
None
"""
return if_function(cond(), true_func(), false_func()) #将cond()函数、true_func()函数、false_func()函数的结果传递给if_function(),所有三个函数被调用,它们的结果被返回到一个函数,作为if_function的参数
def cond():
return False
def true_func():
print(42)
def false_func():
print(47)
- 理解
with_if_statement()
和with_if_function()
的中的调用函数区别,在代码中进行了注释。 - 考虑非纯函数的副作用(side effect),导致
with_if_statement()
和with_if_function()
的结果不同且print(result)
为None
Q6: Hailstone
def hailstone(n):
"""Print the hailstone sequence starting at n and return its
length.
>>> a = hailstone(10)
10
5
16
8
4
2
1
>>> a
7
"""
k = 1
while n != 1:
print(n)
if n%2 == 0:
n = n//2
else:
n = n*3+1
k += 1
print(n)
return k
- 注意到
>>> a = hailstone(10)
10
5
16
8
4
2
1
hailstone()
非纯函数,a = hailstone(10)
调用了hailstone()
函数,显示的是hailstone()
函数调用这一过程,调用完后其返回的值储存在a
中。- 注意使用
//
去掉小数