Q1: Taxicab Distance
这个问题比较简单,street()和avenue()都已经定义好了, 直接调用计算距离即可
def taxicab(a, b):
return abs(street(a) - street(b)) + abs(avenue(a) - avenue(b))
Q2: Squares only
Implement the function squares
, which takes in a list of positive integers. It returns a list that contains the square roots of the elements of the original list that are perfect squares.
求列表里的完全平方数, 只需遍历一遍列表, 元素判断为完全平方数则存入list里面
def squares(s):
return [round(math.sqrt(x)) for x in s if square(round(math.sqrt(x))) == x]
Q3: G function
A mathematical function G
on positive integers is defined by two cases:
G(n) = n, if n <= 3
G(n) = G(n - 1) + 2 * G(n - 2) + 3 * G(n - 3), if n > 3
Write a recursive function g
that computes G(n)
. Then, write an iterative function g_iter
that also computes G(n)
:
def g(n):
if n <= 3:
return n
return g(n-1) + 2*g(n-2) + 3*g(n-3)
def g_iter():
if n <= 3:
return n
g1, g2, g3 = 1, 2, 3
for i in range(4, n+1):
g4 = g3 + 2*g2 + 3*g1
g1, g2, g3 = g2, g3, g4
return g3
Q4: Ping pong
The ping-pong sequence counts up starting from 1 and is always either counting up or counting down. At element k
, the direction switches if k
is a multiple of 7 or contains the digit 7. The first 30 elements of the ping-pong sequence are listed below, with direction swaps marked using brackets at the 7th, 14th, 17th, 21st, 27th, and 28th elements:
1 2 3 4 5 6 [7] 6 5 4 3 2 1 [0] 1 2 [3] 2 1 0 [-1] 0 1 2 3 4 [5] [4] 5 6
Implement a function pingpong
that returns the nth element of the ping-pong sequence. Do not use any assignment statements; however, you may use def
statements.
这道题只要理解了意思还是不难实现的,而且已经给出了提示,虽然不能赋值但是可以将迭代的中间变量作为函数的参数进行“伪递归"
def pingpong(n):
def iter(i, step, times):if times == n:
return i
if has_seven(times) or times % 7 == 0:
return iter(i-step, -step, times+1)
else:
return iter(i+step, step, times+1)
return iter(1, 1, 1)
Q5: Count change
Once the machines take over, the denomination of every coin will be a power of two: 1-cent, 2-cent, 4-cent, 8-cent, 16-cent, etc. There will be no limit to how much a coin can be worth.
A set of coins makes change for amount
if the sum of the values of the coins is amount
. For example, the following sets make change for 7
:
- 7 1-cent coins
- 5 1-cent, 1 2-cent coins
- 3 1-cent, 2 2-cent coins
- 3 1-cent, 1 4-cent coins
- 1 1-cent, 3 2-cent coins
- 1 1-cent, 1 2-cent, 1 4-cent coins
Thus, there are 6 ways to make change for 7
. Write a function count_change
that takes a positive integer amount
and returns the number of ways to make change for amount
using these coins of the future:
这道题的思路是使用dfs,将money的大小进行*2递增迭代。func(remain-money, money)表示兑换这种money, 并且剩余的钱变为remain-money; func(remain, money*2)表示不兑换这种钱, 并且选择下一种money。这两个函数加起来即为最终的兑换方式个数。
def count_change(amount):
if money > amount:
return 0
elif remain < 0:
return 0
elif remain == 0:
return 1
else:
return func(remain - money, money) + func(remain, money * 2)
return func(amount, 1)
Q6: Anonymous factorial
The recursive factorial function can be written as a single expression by using a conditional expression.
>>> fact = lambda n: 1 if n == 1 else mul(n, fact(sub(n, 1)))
>>> fact(5)
120
However, this implementation relies on the fact (no pun intended) that fact
has a name, to which we refer in the body of fact
. To write a recursive function, we have always given it a name using a def
or assignment statement so that we can refer to the function within its own body. In this question, your job is to define fact recursively without giving it a name!
Write an expression that computes n
factorial using only call expressions, conditional expressions, and lambda expressions (no assignment or def statements). Note in particular that you are not allowed to use make_anonymous_factorial
in your return expression. The sub
and mul
functions from the operator
module are the only built-in functions required to solve this problem:
这道题为extra quetion, 想了好久还是没有想出来, 在网上也没有找到答案,最后干脆放弃了。。
总结
总的来说这次hw比较简单,所以花的时间比较少