cs61a 2020fall lab02

cs61a 2020fall lab02

(¦3[▓▓]


def lambda_curry2(func):
    """
    Returns a Curried version of a two-argument function FUNC.
    >>> from operator import add, mul, mod
    >>> curried_add = lambda_curry2(add)
    >>> add_three = curried_add(3)
    >>> add_three(5)
    8
    >>> curried_mul = lambda_curry2(mul)
    >>> mul_5 = curried_mul(5)
    >>> mul_5(42)
    210
    >>> lambda_curry2(mod)(123)(10)
    3
    """
    "*** YOUR CODE HERE ***"
    def g(x):
        def h(y):
            return func(x,y)
        return h



    return g



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_factors = count_cond(lambda n, i: n % i == 0)
    >>> count_factors(2)   # 1, 2
    2
    >>> count_factors(4)   # 1, 2, 4
    3
    >>> count_factors(12)  # 1, 2, 3, 4, 6, 12
    6

    >>> is_prime = lambda n, i: count_factors(i) == 2
    >>> count_primes = count_cond(is_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
    """
    "*** YOUR CODE HERE ***"
    def h(n):
        i,count = 1,0
        while i <= n:
            if condition(n,i):
                count+=1
            i+=1
        return count
    return h


def compose1(f, g):
    """Return the composition function which given x, computes f(g(x)).

    >>> add_one = lambda x: x + 1        # adds one to x
    >>> square = lambda x: x**2
    >>> a1 = compose1(square, add_one)   # (x + 1)^2
    >>> a1(4)
    25
    >>> mul_three = lambda x: x * 3      # multiplies 3 to x
    >>> a2 = compose1(mul_three, a1)    # ((x + 1)^2) * 3
    >>> a2(4)
    75
    >>> a2(5)
    108
    """
    return lambda x: f(g(x))

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
    >>> b1 = composite_identity(square, add_one)
    >>> b1(0)                            # (0 + 1)^2 == 0^2 + 1
    True
    >>> b1(4)                            # (4 + 1)^2 != 4^2 + 1
    False
    """
    "*** YOUR CODE HERE ***"
    h = compose1(f,g)
    g = compose1(g,f)
    return lambda x:(h(x) == g(x))



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
    """
    "*** YOUR CODE HERE ***"
    def h(n):
        if n == 0:
            return lambda x:x
        else:
            def g(x):
                i = 1
                while i <= n:    
                    if i % 3 == 1:
                        x = f1(x)
                    elif i % 3 == 2:
                        x = f2(x)
                    elif i % 3 == 0:
                        x = f3(x)
                    i = i + 1
                return x
            return g
    return lambda n :h(n)


                            







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CS61A 2020秋季学期的讨论课5主要涉及到关于递归的问题。这节讨论课中我们学习了递归函数的定义、调用和实例,并进行了一些练习。 首先,我们回顾了递归函数的定义。递归函数是指在一个函数的定义中调用了该函数本身的情况。这样的定义允许我们通过将问题分解为更小的子问题来解决复杂的问题。递归函数通常包括一个基本情况和一个递归情况。基本情况表示问题已经足够简单,可以直接计算出结果,而递归情况则表示将问题拆解为更小的子问题,并调用自身来解决这些子问题。 在练习中,我们通过编写递归函数来解决一系列问题。例如,我们实现了一个递归函数来计算一个列表的长度。首先,我们检查基本情况,即当列表为空时长度为0。然后,我们将问题拆解为子问题,即将列表分解为其第一个元素和其余部分。然后,我们递归地计算剩余部分的长度,并将其加上第一个元素,最终得到整个列表的长度。 在讨论课中,我们还学习了尾递归。尾递归是指递归函数中递归调用发生在函数的最后一步操作的情况。尾递归函数可以通过迭代的方式执行,而不会在每次递归调用时创建新的栈帧,从而减少了内存的使用。这对于处理大规模数据非常有用。 总而言之,本次讨论课中我们学习了递归函数的定义和使用。通过理解递归的原理和练习编写递归函数,我们能够更好地解决复杂的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值