《Think Python 2e》作业实现(六): 有返回值的函数

《Think Python 2e》作业实现(六): 有返回值的函数



这是什么?

这里是《Think Python 2e》作业实现 !在这里将记录《Think Python 2e》作业的练习记录、终端信息和结果分析。

  • 这是《Think Python 2e》哪个版本的作业?
    《Think Python:如何像计算机科学家一样思考》第二版。这里主要参考了一个中文网页版《Think Python 2e》中译本
  • 可以当成《Think Python 2e》参考答案吗?
    这里主要记录了我自己完成作业时所产生的成果及习题总结,基本未参考教材所提供的答案,未免有失规范,参考答案建议还是以 绿茶出版社官方代码 为准。
  • 不同的解释器版本结果不尽相同,这里用的哪个版本Python解释器?
    这里用了Python 3.8.6版解释器,部分用安卓Pydroid 4.01_arm64中的3.8.3版Python解释器,在线解释器用教程推荐的PythonAnywhere中的3.8版Python解释器。

习题6-1:画组合函数的堆栈图

【习题】 画出下面程序的堆栈图,这个程序的最终输出是什么?

def b(z):
    prod = a(z, z)
    print(z, prod)
    return prod

def a(x, y):
    x = x + 1
    return x * y

def c(x, y, z):
	total = x + y + z
    square = b(total)**2
    return square

x = 1
y = x + 1
print(c(x, y+3, x+y))
  • 练习记录:
    在这里插入图片描述
PS C:\Users\Administrator> python D:\WorkSpace\thinkpython2e\new27.py
9 90
8100

习题6-2:递归定义函数Ackermann

【习题】 编写一个叫作 ack 的函数来计算 Ackermann 函数,使用你的函数计算 ack(3,4),其结果应该为 125, 如果 m 和 n 的值较大时,会发生什么?

  • 练习记录:
def ack(m, n):
	if m == 0:
		return n + 1
	elif m > 0 and n == 0:
		return ack(m - 1, 1)
	elif m > 0 and n > 0:
		return ack(m - 1, ack(m, n - 1))
		
print(ack(3, 4))
PS C:\Users\Administrator> python D:\WorkSpace\thinkpython2e\new28.py
125
def ack(m, n):
	if m == 0:
		return n + 1
	elif m > 0 and n == 0:
		return ack(m - 1, 1)
	elif m > 0 and n > 0:
		return ack(m - 1, ack(m, n - 1))
		
print(ack(9, 10))
PS C:\Users\Administrator> python D:\WorkSpace\thinkpython2e\new28.py
Traceback (most recent call last):
  File "D:\WorkSpace\thinkpython2e\new28.py", line 11, in <module>
    print(ack(9, 10))
  File "D:\WorkSpace\thinkpython2e\new28.py", line 7, in ack
    return ack(m - 1, ack(m, n - 1))
  File "D:\WorkSpace\thinkpython2e\new28.py", line 7, in ack
    return ack(m - 1, ack(m, n - 1))
  File "D:\WorkSpace\thinkpython2e\new28.py", line 7, in ack
    return ack(m - 1, ack(m, n - 1))
  [Previous line repeated 7 more times]
  File "D:\WorkSpace\thinkpython2e\new28.py", line 5, in ack
    return ack(m - 1, 1)
  File "D:\WorkSpace\thinkpython2e\new28.py", line 7, in ack
    return ack(m - 1, ack(m, n - 1))
  File "D:\WorkSpace\thinkpython2e\new28.py", line 5, in ack
    return ack(m - 1, 1)
  File "D:\WorkSpace\thinkpython2e\new28.py", line 7, in ack
    return ack(m - 1, ack(m, n - 1))
  File "D:\WorkSpace\thinkpython2e\new28.py", line 5, in ack
    return ack(m - 1, 1)
  File "D:\WorkSpace\thinkpython2e\new28.py", line 7, in ack
    return ack(m - 1, ack(m, n - 1))
  File "D:\WorkSpace\thinkpython2e\new28.py", line 5, in ack
    return ack(m - 1, 1)
  File "D:\WorkSpace\thinkpython2e\new28.py", line 7, in ack
    return ack(m - 1, ack(m, n - 1))
  File "D:\WorkSpace\thinkpython2e\new28.py", line 5, in ack
    return ack(m - 1, 1)
  File "D:\WorkSpace\thinkpython2e\new28.py", line 7, in ack
    return ack(m - 1, ack(m, n - 1))
  File "D:\WorkSpace\thinkpython2e\new28.py", line 7, in ack
    return ack(m - 1, ack(m, n - 1))
  File "D:\WorkSpace\thinkpython2e\new28.py", line 7, in ack
    return ack(m - 1, ack(m, n - 1))
  [Previous line repeated 975 more times]
  File "D:\WorkSpace\thinkpython2e\new28.py", line 5, in ack
    return ack(m - 1, 1)
  File "D:\WorkSpace\thinkpython2e\new28.py", line 2, in ack
    if m == 0:
RecursionError: maximum recursion depth exceeded in comparison
  • 结果分析:RecursionError: maximum recursion depth exceeded in comparison显示超过了默认递归深度,默认为1000

习题6-3:检查字符串是不是回文字

【习题6.3.1】 将 first、last 和 middle 三个函数录入到文件 palindrome.py 中并测试,当你用一个两个字母的字符串调用 middle 时会发生什么?一个字母的呢?空字符串呢?空字符串这样'' 表示,中间不含任何字母

  • 练习记录:
def first(word):
    return word[0]

def last(word):
	return word[-1]

def middle(word):
    return word[1:-1]
	
print(middle('wow'))
print(middle('wo'))
print(middle('w'))
print(middle(''))
PS C:\Users\Administrator> python D:\WorkSpace\thinkpython2e\palindrome.py
o


【习题6.3.2】 编写一个叫 is_palindrome 的函数,接受一个字符串作为实参。如果是回文词,就返回 True ,反之则返回 False,记住,你可以使用内建函数 len 来检查字符串的长度

  • 练习记录:
def first(word):
    return word[0]

def last(word):
	return word[-1]

def middle(word):
    return word[1:-1]
	
def is_palindrome(word):
	"""Returns True if word is a palindrome."""
	if len(word) <= 1:
		return True
	else:
		if first(word) == last(word):
			return is_palindrome(middle(word))
		else:
			return False
			
print(is_palindrome('w'))
print(is_palindrome('wo'))
print(is_palindrome('wow'))
print(is_palindrome('wowo'))
print(is_palindrome('wowow'))
PS C:\Users\Administrator> python D:\WorkSpace\thinkpython2e\is_palindrome.py
True
False
True
False
True

习题6-4:检查两个数是不是幂的关系

【习题】 编写一个叫 is_power 的函数,接受两个参数 a 和 b, 并且当 a 是 b 的幂时返回 True

  • 练习记录:
def is_power(a, b):
	if a == 0 or b == 0 or a != int(a) or b != int(b):
		return 'The value given is not a nonzero integer.'
	if (a == 1) or (a == -1 and b == -1):
		return True
	if a/b == int(a/b) and abs(b) != 1:
		return is_power(a/b, b)
	return False

print(is_power(9, 0))
print(is_power(1.69, 1.3))
print(is_power(9, -3))
print(is_power(1, 1))
print(is_power(-1, 1))
print(is_power(1, -1))
print(is_power(-1, -1))
print(is_power(1, 3))
print(is_power(3, 1))
print(is_power(3, -1))
PS C:\Users\Administrator> python D:\WorkSpace\thinkpython2e\is_power.py
The value given is not a nonzero integer.
The value given is not a nonzero integer.
True
True
False
True
True
True
False
False
  • 结果分析:1.69/1.3 = ? 这可真是个问题,在数学中精确答案是1.3,所以1.69是1.3的幂;但在 python 中计算时,由于精度所限为1.2999999999999998,如下图,1.69就不是1.3的幂;所以这个函数能接受的数值只能简化为整数
    在这里插入图片描述

习题6-6:求两个数的最大公约数

【习题】 求两个数的最大公约数的一种方法,是基于这样一个原理:如果 r 是 a 被 b 除后的余数,那么 gcd(a,b) = gcd(b, r) ,我们可以把 gcd(a, 0) = a 当做基础情形;编写一个叫 gcd 的函数,接受两个参数 a 和 b,并返回二者的最大公约数

  • 练习记录:
def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a%b)
	
print(gcd(111, 74))
PS C:\Users\Administrator> python D:\WorkSpace\thinkpython2e\gcd.py
37

猜你喜欢

转载自blog.csdn.net/weixin_41217917/article/details/112473444