ICode9

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

2021 fall cs61lab11

2022-04-20 21:02:29  阅读:189  来源: 互联网

标签:src return self current 2021 fall cs61lab11 line first


网址 https://inst.eecs.berkeley.edu/~cs61a/fa21/lab/lab11/#problem-3
problem1:

    class Buffer:
        """A Buffer provides a way of accessing a sequence of tokens across lines.

        Its constructor takes an iterator, called "the source", that returns the
        next line of tokens as a list each time it is queried, or None to indicate
        the end of data.

        The Buffer in effect concatenates the sequences returned from its source
        and then supplies the items from them one at a time through its pop_first()
        method, calling the source for more sequences of items only when needed.

        In addition, Buffer provides a current method to look at the
        next item to be supplied, without sequencing past it.

        The __str__ method prints all tokens read so far, up to the end of the
        current line, and marks the current token with >>.

        >>> buf = Buffer(iter([['(', '+'], [15], [12, ')']]))
        >>> buf.pop_first()
        '('
        >>> buf.pop_first()
        '+'
        >>> buf.current()
        15
        >>> buf.current()   # Calling current twice should not change buf
        15
        >>> buf.pop_first()
        15
        >>> buf.current()
        12
        >>> buf.pop_first()
        12
        >>> buf.pop_first()
        ')'
        >>> buf.pop_first()  # returns None
        """

        def __init__(self, source):
            self.index = 0
            self.source = source#source是每一次next是一个列表
            self.current_line = ()
            self.current()

        def pop_first(self):
            """Remove the next item from self and return it. If self has
            exhausted its source, returns None."""
            # BEGIN PROBLEM 1
            "*** YOUR CODE HERE ***"
            current = self.current()
            self.index += 1#只有每次pop_first才会改变index也就是改变current()
            return current
            # END PROBLEM 1

        def current(self):
            """Return the current element, or None if none exists."""
            while not self.more_on_line():#当more_on_line()函数False时超过了当前索引,就需要进入下一个列表
                self.index = 0
                try:
                    # BEGIN PROBLEM 1
                    "*** YOUR CODE HERE ***"
                    self.current_line = next(self.source)#进入下一个列表
                    # END PROBLEM 1
                except StopIteration:
                    self.current_line = ()
                    return None
            return self.current_line[self.index]

        def more_on_line(self):
            return self.index < len(self.current_line)

problem 2 and 3:
这两道题目就是要对给定的src进行解释,两个函数相互递归,有一些规则
scheme_read遇到左括号就调用read_line函数来生成Pair,遇到'就是引用即解释为quote,
read_line函数就是如果遇到右括号就结束,如果是其他的数字或者字母,布尔值就正常储存,在调用scheme_read函数进入递归

    def scheme_read(src):
        """Read the next expression from SRC, a Buffer of tokens.
        """
        if src.current() is None:
            raise EOFError
        val = src.pop_first()  # Get and remove the first token
        if val == 'nil':
            return nil
        elif val == '(':
            return read_tail(src)
        elif val == "'":
            return Pair('quote', Pair(scheme_read(src), nil))
        elif val not in DELIMITERS:
            return val
        else:
            raise SyntaxError('unexpected token: {0}'.format(val))


    def read_tail(src):
        """Return the remainder of a list in SRC, starting before an element or ).
        """
        try:
            if src.current() is None:
                raise SyntaxError('unexpected end of file')
            elif src.current() == ')':
                src.pop_first()
                return nil
            else:
                return Pair(scheme_read(src), read_tail(src))
        except EOFError:
            raise SyntaxError('unexpected end of file')

标签:src,return,self,current,2021,fall,cs61lab11,line,first
来源: https://www.cnblogs.com/echoT/p/16171686.html

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

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

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

ICode9版权所有