cs61a 2021 fall lab04

网址 https://inst.eecs.berkeley.edu/~cs61a/fa21/lab/lab04/
question1:
简单理解递归就好了
这是一些测试用例

    HW_SOURCE_FILE = __file__


    def summation(n, term):
        """Return the sum of numbers 1 through n (including n) wíth term applied to each number.
        Implement using recursion!

        >>> summation(5, lambda x: x * x * x) # 1^3 + 2^3 + 3^3 + 4^3 + 5^3
        225
        >>> summation(9, lambda x: x + 1) # 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
        54
        >>> summation(5, lambda x: 2**x) # 2^1 + 2^2 + 2^3 + 2^4 + 2^5
        62
        >>> # Do not use while/for loops!
        >>> from construct_check import check
        >>> # ban iteration
        >>> check(HW_SOURCE_FILE, 'summation',
        ...       ['While', 'For'])
        True
        """
        assert n >= 1
        "*** YOUR CODE HERE ***"
        if n == 1:
            return term(1)
        else :
            return term(n) + summation(n-1, term)


    def pascal(row, column):
        """Returns the value of the item in Pascal's Triangle 
        whose position is specified by row and column.
        >>> pascal(0, 0)
        1
        >>> pascal(0, 5)	# Empty entry; outside of Pascal's Triangle
        0
        >>> pascal(3, 2)	# Row 3 (1 3 3 1), Column 2
        3
        >>> pascal(4, 2)     # Row 4 (1 4 6 4 1), Column 2
        6
        """
        "*** YOUR CODE HERE ***"
        if column > row :
            return 0
        if column == 0:
            return 1
        return pascal(row - 1, column) + pascal(row - 1, column - 1)


    def paths(m, n):
        """Return the number of paths from one corner of an
        M by N grid to the opposite corner.

        >>> paths(2, 2)
        2
        >>> paths(5, 7)
        210
        >>> paths(117, 1)
        1
        >>> paths(1, 157)
        1
        """
        "*** YOUR CODE HERE ***"
        if m == 1 or n == 1:
            return 1
        return paths(m - 1, n) + paths(m, n - 1)


    def couple(s, t):
        """Return a list of two-element lists in which the i-th element is [s[i], t[i]].

        >>> a = [1, 2, 3]
        >>> b = [4, 5, 6]
        >>> couple(a, b)
        [[1, 4], [2, 5], [3, 6]]
        >>> c = ['c', 6]
        >>> d = ['s', '1']
        >>> couple(c, d)
        [['c', 's'], [6, '1']]
        """
        assert len(s) == len(t)
        "*** YOUR CODE HERE ***"
        for i in range(len(s)):
            s[i] = [s[i], t[i]]
        return s


    def coords(fn, seq, lower, upper):
        """
        >>> seq = [-4, -2, 0, 1, 3]
        >>> fn = lambda x: x**2
        >>> coords(fn, seq, 1, 9)
        [[-2, 4], [1, 1], [3, 9]]
        """
        "*** YOUR CODE HERE ***"
        return [[x,fn(x)] for x in seq if lower <= fn(x) <= upper]


    def riffle(deck):
        """Produces a single, perfect riffle shuffle of DECK, consisting of
        DECK[0], DECK[M], DECK[1], DECK[M+1], ... where M is position of the
        second half of the deck.  Assume that len(DECK) is even.
        >>> riffle([3, 4, 5, 6])
        [3, 5, 4, 6]
        >>> riffle(range(20))
        [0, 10, 1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19]
        """
        "*** YOUR CODE HERE ***"
        ans = []
        even = 0
        odd = int(len(deck) / 2)
        for i in range(0,len(deck)):
            if i % 2 == 0:
                ans = ans + [deck[even]]
                even += 1
            else:
                ans = ans + [deck[odd]]
                odd += 1
        return ans
posted @ 2022-03-20 13:52  天然气之子  阅读(276)  评论(0编辑  收藏  举报