ICode9

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

2021 fall cs61a dis14

2022-05-03 19:01:10  阅读:348  来源: 互联网

标签:return Tree list cs61a Link 2021 True dis14 greetings


网址 https://inst.eecs.berkeley.edu/~cs61a/fa21/disc/disc14/

目录

problem1:

就是两种情况考虑,然后加起来就好了

    def paths(x, y):
        """Return a list of ways to reach y from x by repeated
        incrementing or doubling.
        >>> paths(3, 5)
        [[3, 4, 5]]
        >>> sorted(paths(3, 6))
        [[3, 4, 5, 6], [3, 6]]
        >>> sorted(paths(3, 9))
        [[3, 4, 5, 6, 7, 8, 9], [3, 4, 8, 9], [3, 6, 7, 8, 9]]
        >>> paths(3, 3) # No calls is a valid path
        [[3]]
        """
        if x == y:
            return [[x]]
        elif x > y:
            return []
        else:
            a = paths(x + 1, y)
            b = paths(x * 2, y)
            return [[x] + p for p in a + b]

problem2:

    def reverse(lst):
        """Reverses lst using mutation.

        >>> original_list = [5, -1, 29, 0]
        >>> reverse(original_list)
        >>> original_list
        [0, 29, -1, 5]
        >>> odd_list = [42, 72, -8]
        >>> reverse(odd_list)
        >>> odd_list
        [-8, 72, 42]
        """
        "*** YOUR CODE HERE ***"
        for i in range(len(lst) // 2):
            temp = lst[i]
            lst[i] = lst[-(i+1)]
            lst[-(i+1)] = temp

problem3:

    def reverse_other(t):
        """Mutates the tree such that nodes on every other (odd-depth)
        level have the labels of their branches all reversed.

        >>> t = Tree(1, [Tree(2), Tree(3), Tree(4)])
        >>> reverse_other(t)
        >>> t
        Tree(1, [Tree(4), Tree(3), Tree(2)])
        >>> t = Tree(1, [Tree(2, [Tree(3, [Tree(4), Tree(5)]), Tree(6, [Tree(7)])]), Tree(8)])
        >>> reverse_other(t)
        >>> t
        Tree(1, [Tree(8, [Tree(3, [Tree(5), Tree(4)]), Tree(6, [Tree(7)])]), Tree(2)])
        """
        "*** YOUR CODE HERE ***"
        def help(t, flag):
            if t.is_leaf():
                return
            if flag :
                for i in range(len(t.branches) // 2):
                    temp = t.branches[i].label
                    t.branches[i].label= t.branches[-(i+1)].label
                    t.branches[-(i+1)].label = temp
            if flag:
                flag = False
            else: 
                flag = True
            for i in t.branches:
                help(i, flag)
        return help(t, True)

problem4:

    def deep_map(f, link):
        """Return a Link with the same structure as link but with fn mapped over
        its elements. If an element is an instance of a linked list, recursively
        apply f inside that linked list as well.

        >>> s = Link(1, Link(Link(2, Link(3)), Link(4)))
        >>> print(deep_map(lambda x: x * x, s))
        <1 <4 9> 16>
        >>> print(s) # unchanged
        <1 <2 3> 4>
        >>> print(deep_map(lambda x: 2 * x, Link(s, Link(Link(Link(5))))))
        <<2 <4 6> 8> <<10>>>
        """
        "*** YOUR CODE HERE ***"
        if link is Link.empty:
            return link
        if isinstance(link.first, Link):
            first = deep_map(f, link.first)
        else:  
            first = f(link.first)
        return Link(first, deep_map(f, link.rest))

probelm5:

    def repeated(f):
        """
        >>> double = lambda x: 2 * x
        >>> funcs = repeated(double)
        >>> identity = next(funcs)
        >>> double = next(funcs)
        >>> quad = next(funcs)
        >>> oct = next(funcs)
        >>> quad(1)
        4
        >>> oct(1)
        8
        >>> [g(1) for _, g in
        ...  zip(range(5), repeated(lambda x: 2 * x))]
        [1, 2, 4, 8, 16]
        """

        g = lambda x : x
        while True:
            yield g
            g = (lambda g: lambda x: f(g(x)))(g)

probelm 6:

这个和lab14的question2很像

    (define (nondecreaselist s)

        (if (null? s)
            nil
            (let ((rest (nondecreaselist (cdr s)) ))
                (if (or (null? (cdr s)) (> (car s) (car (cdr s))))
                    (cons (list (car s)) rest)
                    (cons (cons (car s) (car rest)) (cdr rest))
                )
            )
        )

problem7:

    import re

    def greetings(message):
        """
        Returns whether a string is a greeting. Greetings begin with either Hi, Hello, or
        Hey (either capitalized or lowercase), and/or end with Bye (either capitalized or lowercase) optionally followed by
        an exclamation point or period.

        >>> greetings("Hi! Let's talk about our favorite submissions to the Scheme Art Contest")
        True
        >>> greetings("Hey I just figured out that when I type the Konami Code into cs61a.org, something fun happens")
        True
        >>> greetings("I'm going to watch the sun set from the top of the Campanile! Bye!")
        True
        >>> greetings("Bye Bye Birdie is one of my favorite musicals.")
        False
        >>> greetings("High in the hills of Berkeley lived a legendary creature. His name was Oski")
        False
        >>> greetings('Hi!')
        True
        >>> greetings("bye")
        True
        """
        return bool(re.search(r'((^(Hi)|(Hey)|(hello))\b)|(\b([bB]ye)[!\.]?)$', message))

problem9and10

    )

标签:return,Tree,list,cs61a,Link,2021,True,dis14,greetings
来源: https://www.cnblogs.com/echoT/p/16219029.html

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

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

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

ICode9版权所有