eachconfig = [['a', ['blank', ['p1', 'r', 'r'], ['b']]], ['b', ['blank', ['p0'], ['c']]], ['c', ['blank', ['r', 'r'], ['a']]]]
def turingmachine(data):
a = 0
tape = []
finalmc = data[0][0]
for z in range(1,40):
tape.append(' ')
mcName = []
m = 0
for emc in range(1,len(data)+1):
goal = data[m][0]
mcName.append(goal)
m+=1
mcNumber = [h-1 for h in range(1,len(mcName)+1)]
mcNameNumber = dict(zip(mcName,mcNumber))
d = 0
tapeposition = d
tapescan = tape[d]
for process in range(1,len(tape)):
b = 0
c = 0
cconfig = data[a][b]
if cconfig == finalmc:
b += 1
scannedsymbol = data[a][b][c]
if isinstance(scannedsymbol, str):
if scannedsymbol.lower() in ('any', 'blank'):
c += 1
operations = data[a][b][c]
for cycle in operations:
if cycle[0] in ('p','P'):
tape[tapeposition] = cycle[1:]
elif cycle[0] in ('r', 'R'):
tapeposition += 1
elif cycle[0] in ('l', 'L'):
tapeposition -= 1
elif cycle[0] in ('e', 'E'):
tape[tapeposition] == ' '
finalmc = data[a][b][-1][0]
a = mcNameNumber[finalmc]
return tape
print turingmachine(eachconfig)
这段代码我的效率可能并不惊人,也不完整,它只是当前'eachconfig'列表中设置数据的一个框架。尽管如此,程序的输出仍然只是[1, ' ', ' ', ' ', ...]
,它应该在哪里[1, ' ' , 0 , ' ' , 1 , ' ' , 0 , ...]
我已经阅读了数十次代码,我通过打印出每一行来检查错误,我发现问题在于它只迭代循环一次(有问题的循环是for process in range(1,len(data)+1):
循环,之前的循环仅用于初始化),但我不知道为什么。为什么会这样?