2021 fall cs61lab11
2022/4/20 23:20:44
本文主要是介绍2021 fall cs61lab11,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
网址 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')
这篇关于2021 fall cs61lab11的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南
- 2025-01-03图像文字理解,OCR、大模型还是多模态模型?PalliGema2在QLoRA技术上的微调与应用
- 2025-01-03混合搜索:用LanceDB实现语义和关键词结合的搜索技术(应用于实际项目)
- 2025-01-03停止思考数据管道,开始构建数据平台:介绍Analytics Engineering Framework
- 2025-01-03如果 Azure-Samples/aks-store-demo 使用了 Score 会怎样?
- 2025-01-03Apache Flink概述:实时数据处理的利器