1

私は奇妙な問題に遭遇しました。このコードは、正しいブランチに入り、Trueと評価されても、TrueではなくNoneを返します。

edges = { (1, 'a') : [2, 3],
          (2, 'a') : [2],
          (3, 'b') : [4, 3],
          (4, 'c') : [5] }
accepting = [2, 5] 
loc = []
def nfsmsim(string, current, edges, accepting): 

    if string != "":
        if ((current, string[0]) in edges.keys()):
            global loc 
            loc = edges[(current, string[0])]
            print "edge found:",loc

    if (string == ""):
        print "string is over",current,accepting
        print type(current), type(accepting)
        if current in accepting : 
            print "1"
            return True
        else: 
            print "2"
            return 2
    # fill in your code here 
    elif (current, string[0]) in edges.keys():
        global loc
        string = string[1:]
        nfsmsim(string, loc[0], edges, accepting)
    elif len(loc)>1:
        global loc
        nfsmsim(string, loc[1], edges, accepting)


# This problem includes some test cases to help you tell if you are on
# the right track. You may want to make your own additional tests as well.
print nfsmsim("abc", 1, edges, accepting)

これの出力は次のとおりです。

 string is over 5 [2, 5]
<type 'int'> <type 'list'>
1
None (<< instead of True)
4

2 に答える 2

9

これは再帰関数です。ターミナルケース()に到達すると、またはstring == ""に戻ります。これは、呼び出し元の関数(の前の呼び出し)に返されます。しかし、その呼び出しは何も返しません!のターミナルコールから値を取得し、再度返すことで値を渡す必要があります。12nfsmsimnfsmsimnfsmsim

つまり、ステートメントの次の2つのブランチのそれぞれにreturnステートメントが必要ですif

elif (current, string[0]) in edges.keys():
    global loc
    string = string[1:]
    nfsmsim(string, loc[0], edges, accepting)
elif len(loc)>1:
    global loc
    nfsmsim(string, loc[1], edges, accepting)
于 2012-04-23T04:38:19.117 に答える
2

関数の終了時にreturnコマンドを使用しないことは、returnNoneを使用することと同じです。

関数は再帰的であり、その結果を使用しているため、その本体内でもすべての呼び出しの値を返す必要があります。

elif (current, string[0]) in edges.keys():
    global loc
    string = string[1:]
    return nfsmsim(string, loc[0], edges, accepting)
elif len(loc)>1:
    global loc
    return nfsmsim(string, loc[1], edges, accepting)

グローバルlocの使用を忘れる必要があります。引数を介して渡すだけです。とにかくそれは参照です:

edges = { (1, 'a') : [2, 3],
          (2, 'a') : [2],
          (3, 'b') : [4, 3],
          (4, 'c') : [5] }
accepting = [2, 5] 
loc = []
def nfsmsim(string, current, edges, accepting, loc): 

    if string != "":
        if ((current, string[0]) in edges.keys()):
            loc = edges[(current, string[0])]
            print "edge found:",loc

    if (string == ""):
        print "string is over",current,accepting
        print type(current), type(accepting)
        if current in accepting : 
            print "1"
            return True
        else: 
            print "2"
            return 2
    # fill in your code here 
    elif (current, string[0]) in edges.keys():
        string = string[1:]
        return nfsmsim(string, loc[0], edges, accepting, loc)
    elif len(loc)>1:
        return nfsmsim(string, loc[1], edges, accepting, loc)


# This problem includes some test cases to help you tell if you are on
# the right track. You may want to make your own additional tests as well.
print nfsmsim("abc", 1, edges, accepting, loc)

それは私のコンソールに以下を印刷します:

c:\tmp\___python\fixxxer\so10274792>python a.py
edge found: [2, 3]
edge found: [4, 3]
edge found: [5]
string is over 5 [2, 5]
<type 'int'> <type 'list'>
1
True
于 2012-04-23T08:55:04.497 に答える