2016-10-15 5 views
1

私はロボットの動きを刺激する問題を解決しようとしています。ロボットは位置(0、0、 'N')で始まります。コマンドは文字列のリストで与えられます。 'turn'関数はNからE、SからW、そしてNに戻ります。移動関数は特定の方向に移動します.N、Sはy軸、E、Wはx軸です。 N:y + 1 S:y-1 W:x-1 E:x + 1Pythonでの再帰の使い方

機能にショートカットを使用しようとすると、困っている部分があります。

print(macro_interpreter(['turnleft', 'turnright'], {'turnleft': ['turn', 'turn', 'turn'], 'turnright' : ['turn'], 'bigleftturn' : ['move', 'move', 'turnleft', 'move', 'move'], 'bigrightturn' : ['move', 'move', 'turnright', 'move', 'move']})) 

の定義:代わりに 'ターン'

def macro_interpreter(code, macros): 

の 'turnleft' の代わりの[ 'ターン'、 'ターン'、 'ターン'] 'turnright' を使用すると、ときに関数を呼び出しますその用語は辞書で与えられる。 私のコードは、最初のコマンドのために実行し、その後終了し、それがリスト

def macro_interpreter(code, macros): 
    x,y,index = 0, 0, 0 
    state = ['N', 'E', 'S', 'W'] 
    for command in code: 
     if command in macros: 
      return macro_interpreter(macros[command], macros) 
     else: 
      if command == 'move': 
       if state[index] == 'N': 
        y += 1 
       elif state[index] == 'E': 
        x += 1 
       elif state[index] == 'S': 
        y -= 1 
       elif state[index] == 'W': 
        x -= 1 
      elif command == 'turn': 
       try: 
        index = index + 1 
       except IndexError: 
        index = 0 
    return (x, y, state[index])    
+0

期待される出力は?私の 'turn'があなたのマクロ(2回目の実行時)に存在せず、' move''も返されたコードの一部ではないので、そのブロックの下の他の条件は決して実行されません。また、数値をインクリメントすると、IndexErrorが発生することはなく、欠落しているインデックスのリストにアクセスしようとします。 – danidee

答えて

0

第二のコードを無視する行を

return macro_interpreter (macros etc. 

まさにそれを行います。それはforループを残し、外側の呼び出しから戻ります。物語の終わり。

2

codeコマンドで常にループ内の1つのelseステートメントをヒットした場合は、command not in macrosのため再発しません。最初の反復の後

code == ['turn', 'turn', 'turn']、しかしmacrosは、あなたがより正確にこれを行いたい場合、あなたのように、x、y、および「方向インデックス状態」に沿って渡すことができ"turn"


どのキーが含まれていません関数のローカル変数を変更するだけではなく、(0,0、0)に戻して再帰呼び出し内の変数をインクリメント/変更します。何はIndexErrorが、だから私はので、それはだ疑いがある。この

state = ['N', 'E', 'S', 'W'] 
def macro_interpreter(code, macros, x=0,y=0,index=0): 
    # TODO: check if x or y have gone outside "the board" 
     # TODO: return to break from recursion 

    for command in code: 
     if command in macros: 
      return macro_interpreter(macros[command], macros,x,y,index) 
     else: 
      if command == 'move': 
       if state[index] == 'N': 
        return macro_interpreter(code[1:], macros,x,y=y+1,index) 
0

のようなものを数


をインクリメントすることによってキャッチされようとしていないので

はまた、あなたはそれがindex = (index + 1) % len(state)を除いてみ交換する必要がありますあなたが返す

macro_interpreter(macros[command], macros) 

これは単純にfunctiを終了します。再帰コードが実行されるとオンになります。あなたはコードがあなたはそれが何をしたいのか、印刷することが

print macro_interpreter(macros[command], macros) 

return macro_interpreter(macros[command], macros) 

を変更する場合は、表示されます。実際に出力を処理する方法はあなた次第です。

1

再帰を適切にサポートするために私があなたのコードで行ったいくつかの修正があります。このコードは、あなたが習得したいものです。今

def macro_interpreter(code, macros, x=0, y=0, index=0): 
    state = ['N', 'E', 'S', 'W'] 
    for command in code: 
     if command in macros: 
      x, y, curr_state = macro_interpreter(macros[command], macros, x, y, index) 
      # update new index with new state value   
      index = state.index(curr_state) 
     else: 
      if command == 'move': 
       if state[index] == 'N': 
        y += 1 
       elif state[index] == 'E': 
        x += 1 
       elif state[index] == 'S': 
        y -= 1 
       elif state[index] == 'W': 
        x -= 1 
      elif command == 'turn':     
       index = (index + 1)%len(state) 
    return (x, y, state[index]) 

、私はあなたのテストケース

>> print macro_interpreter(['turnleft', 'turnright'], {'turnleft': ['turn', 'turn', 'turn'], 'turnright' : ['turn'], 'bigleftturn' : ['move', 'move', 'turnleft', 'move', 'move'], 'bigrightturn' : ['move', 'move', 'turnright', 'move', 'move']}) 
Output:- (0, 0, 'N') 

を実行する場合、私は、この意志はあなたを助け願っています。