不匹配输出与正确的输出

时间:2016-10-12 02:59:43

标签: python

enter image description here

def interpret(result : [None]) -> str:
    s = ''
    s = s + 'Start state = ' + result[0] +'\n '
    for x in result[1:-1]:
        s += ' Input = ' + x[0]+ '; new possible states = ' + str(sorted(x[1])) + '\n '
    s = s + ' Input = ' + x[0]+ '; new possible states = ' + str(sorted(result[-1][1])) + '\n'
    s = s + 'Stop state(s) = ' + str(sorted(result[-1][1])) 
    return s




           evaluated: Start state = start
  Input = 1; new possible states = ['start']
  Input = 0; new possible states = ['near', 'start']
  Input = 1; new possible states = ['end', 'start']
  Input = 1; new possible states = ['start']
  Input = 0; new possible states = ['near', 'start']
  Input = 0; new possible states = ['end', 'start']
Stop state(s) = ['end', 'start'] == Start state = start
  Input = 1; new possible states = ['start']
  Input = 0; new possible states = ['near', 'start']
  Input = 1; new possible states = ['end', 'start']
  Input = 1; new possible states = ['start']
  Input = 0; new possible states = ['near', 'start']
  Input = 1; new possible states = ['end', 'start']
Stop state(s) = ['end', 'start']

31 *错误:失败ndfa.interpret(i) == "Start state = start\n Input = 1; new possible states = ['start']\n Input = 0; new possible states = ['near', 'start']\n Input = 1; new possible states = ['end', 'start']\n Input = 1; new possible states = ['start']\n Input = 0; new possible states = ['near', 'start']\n Input = 1; new possible states = ['end', 'start']\nStop state(s) = ['end', 'start']\n"

该函数接受一个列表并返回一个字符串。我需要以正确的格式返回字符串。 在我看来,我得到的输出与正确的输出相同,但它给我一个错误。有人能告诉我为什么它不正确吗?非常感谢

输入为['start', ('1', {'start'}), ('0', {'start', 'near'}), ('1', {'end', 'start'}), ('1', {'start'}), ('0', {'start', 'near'}), ('1', {'end', 'start'})]

正确的输出是'"开始状态=开始\ n输入= 1;新的可能状态= [' start'] \ n输入= 0;新的可能状态= [' near',' start'] \ n输入= 1;新的可能状态= ['结束','开始'] \ n输入= 1;新的可能状态= [' start'] \ n输入= 0;新的可能状态= [' near',' start'] \ n输入= 1;新的可能状态= ['结束','开始'] \ n停止状态= ['结束','开始'] \ N'#34;`

我添加了一张图片来显示我得到的结果。

1 个答案:

答案 0 :(得分:0)

我已将该功能的第6行从x[0]更改为result[-1][0],这似乎有效:

def interpret(result : [None]) -> str:
    s = ''
    s = s + 'Start state = ' + result[0] +'\n '
    for x in result[1:-1]:
        s += ' Input = ' + x[0]+ '; new possible states = ' + str(sorted(x[1])) + '\n '
    s += ' Input = ' + result[-1][0] + '; new possible states = ' + str(sorted(result[-1][1])) + '\n'
    s += 'Stop state(s) = ' + str(sorted(result[-1][1])) 
    return s