ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

cs61a 2021 fall lab04

2022-03-20 14:03:47  阅读:331  来源: 互联网

标签:paths CODE return column pascal lab04 2021 fall YOUR


网址 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

标签:paths,CODE,return,column,pascal,lab04,2021,fall,YOUR
来源: https://www.cnblogs.com/echoT/p/16029787.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有