Python 编写Smallf*ck解释器
我想解决的问题是,卡塔: 我通过了124个测试中的123个,但我失败的一个与我如何处理嵌套循环有关。我只是想不出一个复制问题的方法,因为他们没有向您显示测试输入。感谢您的帮助;到目前为止,我对这个问题的看法如下:Python 编写Smallf*ck解释器,python,algorithm,interpreter,Python,Algorithm,Interpreter,我想解决的问题是,卡塔: 我通过了124个测试中的123个,但我失败的一个与我如何处理嵌套循环有关。我只是想不出一个复制问题的方法,因为他们没有向您显示测试输入。感谢您的帮助;到目前为止,我对这个问题的看法如下: import re def interpreter(code, tape): #Filter out non-command characters code_pure = re.findall(r"(\>|\<|\*|\[|\])",
import re
def interpreter(code, tape):
#Filter out non-command characters
code_pure = re.findall(r"(\>|\<|\*|\[|\])", code)
sf = "".join(code_pure)
#Convert tape to list so that elements are mutable
tape_list = []
for bit in tape:
tape_list.append(bit)
#Keep track of pointer and instruction position
pointer = 0
instr = 0
brack_pos = [] #Contains positions of "[" brackets
while instr < len(sf):
#If pointer goes out of bounds then end the program
if pointer >= len(tape_list) or pointer < 0:
return "".join(tape_list)
#"*" flips the current bit
if sf[instr] == "*":
if tape_list[pointer] == "1":
tape_list[pointer] = "0"
elif tape_list[pointer] == "0":
tape_list[pointer] = "1"
instr += 1
#Move right one bit
elif sf[instr] == ">":
pointer += 1
instr += 1
#Move left one bit
elif sf[instr] == "<":
pointer -= 1
instr += 1
elif sf[instr] == "[":
#If pointer is on 0, skip the loop
if tape_list[pointer] == "0":
brack_cnt = 1
while brack_cnt != 0:
instr += 1
if sf[instr] == "[":
brack_cnt += 1
elif sf[instr] == "]":
brack_cnt -= 1
instr += 1
#If pointer is 1, step into the loop
elif tape_list[pointer] != "0":
brack_pos.append(instr)
instr += 1
elif sf[instr] == "]":
if tape_list[pointer] == "0":
instr += 1
elif tape_list[pointer] != "0":
instr = brack_pos[-1]
brack_pos.pop()
return "".join(tape_list)
重新导入
def解释器(代码、磁带):
#过滤掉非命令字符
代码\u pure=re.findall(r“(\>\124;\”):
指针+=1
仪表+=1
#向左移动一点
elif sf[instr]==“问题在于最后一个if块检查sf[instr]==”
中的brack_pos.pop()
仅在循环回开始括号时执行,而不是在移动过结束括号时执行。即brack_pos.pop()
需要无条件执行
顺便说一句,这似乎是一个有趣的练习,我想到了以下几点:
def interpreter(code, tape):
# Implement your interpreter here
tape = list(tape)
# Find matching brackets
jump = [0]*len(code)
open_bracket_stack = []
for i,c in enumerate(code):
if c == '[':
open_bracket_stack.append(i)
elif c == ']':
matching = open_bracket_stack.pop()
jump[i] = matching
jump[matching] = i
code_ptr = 0
tape_ptr = 0
while(code_ptr < len(code) and tape_ptr < len(tape) and tape_ptr >= 0):
c = code[code_ptr]
if c == '>':
tape_ptr += 1
elif c == '<':
tape_ptr -= 1
elif c == '*':
tape[tape_ptr] = '1' if tape[tape_ptr] == '0' else '0'
elif c == '[' and tape[tape_ptr] == '0':
code_ptr = jump[code_ptr]
elif c == ']' and tape[tape_ptr] == '1':
code_ptr = jump[code_ptr]
code_ptr += 1
return "".join(tape)
def解释器(代码、磁带):
#在这里实现您的解释器
磁带=列表(磁带)
#找到匹配的括号
跳转=[0]*len(代码)
打开_括号_堆栈=[]
对于枚举中的i,c(代码):
如果c=='[':
打开\u括号\u堆栈。追加(i)
elif c==']':
匹配=打开\括号\堆栈.pop()
跳转[i]=匹配
跳转[匹配]=i
代码\u ptr=0
磁带\u ptr=0
而(代码ptr=0):
c=代码[code_ptr]
如果c=='>':
磁带ptr+=1
elif c=='请将部分作业(或相关内容)也放在问题中。该链接可能不会在将来工作,但因此,您的问题将永远存在。:)我想我看到了问题。在最后一个if块中,您正在检查sf[instr]==“]”“
您永远不会删除相应的”[“-移动经过“]”时的位置。因此,在嵌套循环中,外部循环将跳回内部“[”。即brack_pos.pop()
在这两种情况下都需要发生。@非常感谢!这确实是个问题,现在我通过了所有测试用例。如果您想将您的评论作为实际解决方案发布,我很乐意将其标记为正确。再次感谢!@Elision当然可以