其他分享
首页 > 其他分享> > 2021 fall cs61a hw05

2021 fall cs61a hw05

作者:互联网

网址 https://inst.eecs.berkeley.edu/~cs61a/fa21/hw/hw05/#required-questions

    def gen_perms(seq):
        """Generates all permutations of the given sequence. Each permutation is a
        list of the elements in SEQ in a different order. The permutations may be
        yielded in any order.

        >>> perms = gen_perms([100])
        >>> type(perms)
        <class 'generator'>
        >>> next(perms)
        [100]
        >>> try: #this piece of code prints "No more permutations!" if calling next would cause an error
        ...     next(perms)
        ... except StopIteration:
        ...     print('No more permutations!')
        No more permutations!
        >>> sorted(gen_perms([1, 2, 3])) # Returns a sorted list containing elements of the generator
        [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
        >>> sorted(gen_perms((10, 20, 30)))
        [[10, 20, 30], [10, 30, 20], [20, 10, 30], [20, 30, 10], [30, 10, 20], [30, 20, 10]]
        >>> sorted(gen_perms("ab"))
        [['a', 'b'], ['b', 'a']]
        """
        "*** YOUR CODE HERE ***"
        if not seq:
            yield []
        else:
            for prem in gen_perms(seq[1:]):
                for i in range(len(seq)):
                    yield prem[:i] + [seq[0]] + prem[i:] 


    def path_yielder(t, value):
        """Yields all possible paths from the root of t to a node with the label
        value as a list.

        >>> t1 = tree(1, [tree(2, [tree(3), tree(4, [tree(6)]), tree(5)]), tree(5)])
        >>> print_tree(t1)
        1
          2
            3
            4
              6
            5
          5
        >>> next(path_yielder(t1, 6))
        [1, 2, 4, 6]
        >>> path_to_5 = path_yielder(t1, 5)
        >>> sorted(list(path_to_5))
        [[1, 2, 5], [1, 5]]

        >>> t2 = tree(0, [tree(2, [t1])])
        >>> print_tree(t2)
        0
          2
            1
              2
                3
                4
                  6
                5
              5
        >>> path_to_2 = path_yielder(t2, 2)
        >>> sorted(list(path_to_2))
        [[0, 2], [0, 2, 1, 2]]
        """
        "*** YOUR CODE HERE ***"
        if label(t) == value:
            yield [label(t)]
        for b in branches(t):
            for  path in path_yielder(b, value):
                yield [label(t)] + path
                "*** YOUR CODE HERE ***"


    def preorder(t):
        """Return a list of the entries in this tree in the order that they
        would be visited by a preorder traversal (see problem description).

        >>> numbers = tree(1, [tree(2), tree(3, [tree(4), tree(5)]), tree(6, [tree(7)])])
        >>> preorder(numbers)
        [1, 2, 3, 4, 5, 6, 7]
        >>> preorder(tree(2, [tree(4, [tree(6)])]))
        [2, 4, 6]
        """
        "*** YOUR CODE HERE ***"
        ans = [label(t)]
        for i in branches(t):
            ans += preorder(i)
        return ans

    def generate_preorder(t):
        """Yield the entries in this tree in the order that they
        would be visited by a preorder traversal (see problem description).

        >>> numbers = tree(1, [tree(2), tree(3, [tree(4), tree(5)]), tree(6, [tree(7)])])
        >>> gen = generate_preorder(numbers)
        >>> next(gen)
        1
        >>> list(gen)
        [2, 3, 4, 5, 6, 7]
        """
        "*** YOUR CODE HERE ***"
        yield from preorder(t)


    def remainders_generator(m):
        """
        Yields m generators. The ith yielded generator yields natural numbers whose
        remainder is i when divided by m.

        >>> import types
        >>> [isinstance(gen, types.GeneratorType) for gen in remainders_generator(5)]
        [True, True, True, True, True]
        >>> remainders_four = remainders_generator(4)
        >>> for i in range(4):
        ...     print("First 3 natural numbers with remainder {0} when divided by 4:".format(i))
        ...     gen = next(remainders_four)
        ...     for _ in range(3):
        ...         print(next(gen))
        First 3 natural numbers with remainder 0 when divided by 4:
        4
        8
        12
        First 3 natural numbers with remainder 1 when divided by 4:
        1
        5
        9
        First 3 natural numbers with remainder 2 when divided by 4:
        2
        6
        10
        First 3 natural numbers with remainder 3 when divided by 4:
        3
        7
        11
        """
        "*** YOUR CODE HERE ***"
        def my(i):
            if i == 0:
                while True:
                    i += m
                    yield i
            j = i
            while True:
                yield j
                j += m
        for i in range(m):
            yield my(i)


    class Tree:
        """
        >>> t = Tree(3, [Tree(2, [Tree(5)]), Tree(4)])
        >>> t.label
        3
        >>> t.branches[0].label
        2
        >>> t.branches[1].is_leaf()
        True
        """

        def __init__(self, label, branches=[]):
            for b in branches:
                assert isinstance(b, Tree)
            self.label = label
            self.branches = list(branches)

        def is_leaf(self):
            return not self.branches

        def __repr__(self):
            if self.branches:
                branch_str = ', ' + repr(self.branches)
            else:
                branch_str = ''
            return 'Tree({0}{1})'.format(self.label, branch_str)

        def __str__(self):
            def print_tree(t, indent=0):
                tree_str = '  ' * indent + str(t.label) + "\n"
                for b in t.branches:
                    tree_str += print_tree(b, indent + 1)
                return tree_str
            return print_tree(self).rstrip()


    tree = lambda label, branches=[]: Tree(label, branches)
    label = lambda t: t.label
    branches = lambda t: t.branches
    print_tree = lambda t: print(t)


    def naturals():
        """A generator function that yields the infinite sequence of natural
        numbers, starting at 1.

        >>> m = naturals()
        >>> type(m)
        <class 'generator'>
        >>> [next(m) for _ in range(10)]
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        """
        i = 1
        while True:
            yield i
            i += 1

标签:branches,hw05,self,tree,label,2021,cs61a,gen,def
来源: https://www.cnblogs.com/echoT/p/16086601.html