Problem 3: Diff of Two Squares (100pts)
Problem
写一个代表变量 \(a\) 和 \(b\) 的平方差 \(a^2−b^2\) 的算术表达式,来替换掉下面
diff_square
函数中的下划线。
def diff_square(a, b):
"""Compute the difference of square a and square b.
Expected result:
>>> diff_square(3, 2)
5
>>> diff_square(3, 4)
-7
"""
return ______
Hints
- 回想一下上课讲了什么。很简单的
Solutions
def diff_square(a, b):
return a ** 2 - b ** 2
def diff_square(a, b):
return a * a - b * b
使用 pow
函数(多此亿举...)
def diff_square(a, b):
return pow(a, 2) - pow(b, 2)