【CS61A】CS61A——Lab2

  • ~3.89K 字
  1. 1. Composite Identity Function
  2. 2. Count Cond
  3. 3. Multiple
  4. 4. I Heard You Liked Functions…

WWPD部分省略

Composite Identity Function

题意:写一个函数,传入f与g两个函数,返回一个含有参数x的函数,用于判断是否等于

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def composite_identity(f, g):
"""
Return a function with one parameter x that returns True if f(g(x)) is
equal to g(f(x)). You can assume the result of g(x) is a valid input for f
and vice versa.

>>> add_one = lambda x: x + 1 # adds one to x
>>> square = lambda x: x**2 # squares x [returns x^2]
>>> b1 = composite_identity(square, add_one)
>>> b1(0) # (0 + 1) ** 2 == 0 ** 2 + 1
True
>>> b1(4) # (4 + 1) ** 2 != 4 ** 2 + 1
False
"""
return lambda x: f(g(x))==g(f(x))

按照题意返回一个lambda函数,传入x即可

Count Cond

predicate function: 返回True或False的函数

题意:写一个函数,传入一个两个参数的predicate function condition,返回一个含有参数n的函数,判断1-n中有几个数满足condition函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def count_cond(condition):
"""Returns a function with one parameter N that counts all the numbers from
1 to N that satisfy the two-argument predicate function Condition, where
the first argument for Condition is N and the second argument is the
number from 1 to N.

>>> count_fives = count_cond(lambda n, i: sum_digits(n * i) == 5)
>>> count_fives(10) # 50 (10 * 5)
1
>>> count_fives(50) # 50 (50 * 1), 500 (50 * 10), 1400 (50 * 28), 2300 (50 * 46)
4

>>> is_i_prime = lambda n, i: is_prime(i) # need to pass 2-argument function into count_cond
>>> count_primes = count_cond(is_i_prime)
>>> count_primes(2) # 2
1
>>> count_primes(3) # 2, 3
2
>>> count_primes(4) # 2, 3
2
>>> count_primes(5) # 2, 3, 5
3
>>> count_primes(20) # 2, 3, 5, 7, 11, 13, 17, 19
8
"""
def judge(n):
cnt = 0
for i in range(1,n+1):
if condition(n,i):
cnt+=1
return cnt
return judge

遍历1-n,传入condition函数并计数即可

注意返回的是函数,传入n

Multiple

题意:写一个函数求参数a,b的最小公倍数

1
2
3
4
5
6
7
8
9
10
11
12
13
def multiple(a, b):
"""Return the smallest number n that is a multiple of both a and b.

>>> multiple(3, 4)
12
>>> multiple(14, 21)
42
"""
def gcd(a,b):
if b==0:
return a
return gcd(b,a%b)
return a*b//gcd(a,b)

辗转相除法求gcd,用gcd求lcm

I Heard You Liked Functions…

题意:定义一个函数传入三个函数f1,f2,f3,返回一个参数为n的函数g,函数g返回一个参数为x的函数h

函数x将会循环传给函数f1,f2,f3,具体如下:

n=0时返回x,n=1时返回f1(x),n=2时返回f2(f1(x)),n=3时返回f3(f2(f1(x))),n=4时返回f1(f3(f2(f1(x)))),以此类推

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def cycle(f1, f2, f3):
"""Returns a function that is itself a higher-order function.

>>> def add1(x):
... return x + 1
>>> def times2(x):
... return x * 2
>>> def add3(x):
... return x + 3
>>> my_cycle = cycle(add1, times2, add3)
>>> identity = my_cycle(0)
>>> identity(5)
5
>>> add_one_then_double = my_cycle(2)
>>> add_one_then_double(1)
4
>>> do_all_functions = my_cycle(3)
>>> do_all_functions(2)
9
>>> do_more_than_a_cycle = my_cycle(4)
>>> do_more_than_a_cycle(2)
10
>>> do_two_cycles = my_cycle(6)
>>> do_two_cycles(1)
19
"""
def g(n):
def h(f,g):
return lambda x: f(g(x))
if n==0:
return lambda x : x
elif n==1:
return f1
else:
temp = f1
i=2
while i<=n:
if i%3==1: temp= h(f1,temp)
elif i%3==2: temp= h(f2,temp)
else: temp= h(f3,temp)
i+=1
return temp
return g

遍历1-n,每次取模3来判断该套哪个函数,注意0和1时特判,2开始从f1往外套

即执行顺序为

赞助喵
非常感谢您的喜欢!
赞助喵