2017-07-18 14 views
0

私はラケットの学習を始めました。私は任意のアーリーツリーと生成的再帰を使用して、ボードからすべてのバージョンのボードを取得しています。Racket - > Python

それでは、私は偽の空を意味し、このボードを持っているとしましょう:

(list "X" "X" "O" "O" "X" "O" "X" #false "X") 

このためのソリューションの要件に応じて次のようになります。

(list 
(list "X" "X" "O" "O" "X" "O" "X" "X" "X") 
(list "X" "X" "O" "O" "X" "O" "X" "O" "X")) 

ラケット内の溶液が素晴らしい作品。私はPythonでも同じことを試みましたが、期待どおりに動作しません。

私はこれらのような出力を得続ける:

[['X', 'X', 'O', 'O', 'X', 'O', 'X', 'X', 'X'], [['X', 'X', 'O', 'O', 'X', 'O', 'X', 'O', 'X'], []]] 

または

['X', 'X', 'O', 'O', 'X', 'O', 'X', 'X', 'X', 'X', 'X', 'O', 'O', 'X', 'O', 'X', 'O', 'X'] 

か悪いかを。

私が望む出力を私に与えることはできないようです。

他に何も動作しない場合は、出力上で後処理を行うことを考えていましたが、避けたいのですが。私は必要なもの

はこれです:いずれの場合で

[['X', 'X', 'O', 'O', 'X', 'O', 'X', 'X', 'X'], ['X', 'X', 'O', 'O', 'X', 'O', 'X', 'O', 'X']] 

、あなたは助けることができるなら、私に知らせてください。

""" 
Brute force solution for tic-tac-toe. 
""" 


""" 
Data definitions: 

;; Value is one of: 
;; - false 
;; - "X" 
;; - "O" 
;; interp. a square is either empty (represented by false) or has and "X" or an "O" 


;; Board is (listof Value) 
;; a board is a list of 9 Values 

""" 

# 
## CONSTANTS 
# 
B0 = [False for i in range(9)] 
B1 = [ 
    False, "X", "O", 
    "O", "X", "O", 
    False, False, "X" 
] 
B2 = [ 
     "X", "X", "O", 
     "O", "X", "O", 
     "X", False, "X", 
     ] 

B3 = [ 
     "X", "O", "X", 
     "O", "O", False, 
     "X", "X", False, 
] 


""" 
PROBLEM 1 

In this problem we want you to design a function that produces all 
possible filled boards that are reachable from the current board. 

In actual tic-tac-toe, O and X alternate playing. For this problem 
you can disregard that. You can also assume that the players keep 
placing Xs and Os after someone has won. This means that boards that 
are completely filled with X, for example, are valid. 

""" 


def fill_board(index, bd): 
    """ 
    Returns a list of 2 board versions with 
    the index filled with "X" and "O" 

    :param index: int; index of position in list to be filled 
    :param bd: Board 
    :return: (listof Board) 
    """ 
    return [ 
     bd[:index] + ["X"] + bd[index+1:], 
     bd[:index] + ["O"] + bd[index + 1:], 
    ] 


assert fill_board(0, B1) == [ 
    [ 
    "X", "X", "O", 
    "O", "X", "O", 
    False, False, "X" 
    ], 
    [ 
    "O", "X", "O", 
    "O", "X", "O", 
    False, False, "X" 
    ], 
] 

assert fill_board(5, B3) == [ 
[ 
     "X", "O", "X", 
     "O", "O", "X", 
     "X", "X", False, 
], 
[ 
     "X", "O", "X", 
     "O", "O", "O", 
     "X", "X", False, 
], 
] 


def find_blank(bd): 
    """ 
    Return the index of the 
    first empty (False) value 
    in the board. 
    ASSUME: there is at least one 
    empty cell. 

    :param bd: Board 
    :return: Index 
    """ 
    return bd.index(False) 

assert find_blank(B0) == 0 
assert find_blank(B2) == 7 
assert find_blank(B3) == 5 


def next_boards(bd): 
    """ 
    Produce the next version of initial board. 
    Finds the first empty (False) cell, and produces 
    2 versions of the board; one with X and one with O 
    :param bd: Board 
    :return: (listof Board) 
    """ 

    return fill_board(find_blank(bd), bd) 


assert next_boards(B0) == [ 
    ["X"] + B0[1:], 
    ["O"] + B0[1:], 
] 


assert next_boards(B3) == [ 
[ 
     "X", "O", "X", 
     "O", "O", "X", 
     "X", "X", False, 
], 
[ 
     "X", "O", "X", 
     "O", "O", "O", 
     "X", "X", False, 
], 
] 


def solve(bd): 
    """ 
    Produce all possible filled boards that are 
    reachable from the current board. 

    :param bd: Board (listof Value) 
    :return: (listof Board) 
    """ 
    def is_full(bd): 
     """ 
     Returns true if board is full; meaning 
     if every value on the board is a string. 

     :param bd: Board (listof Value) 
     :return: Boolean 
     """ 
     return all(type(i) is str for i in bd) 

    def solve__bd(bd): 
     """ 
     Mutually refential function with 
     solve__lobd. This is where all the actual 
     computation takes place. 
     The two functions are responsible for 
     generating and operating on the tree. 
     The tree (arbitraty arity tree) represents 
     another version of the board filled with an 
     additional X or O. 

     :param bd: Board (listof Value) 
     :return: (listof Board) 
     """ 

     if is_full(bd): 
      return list(bd) 

     return solve__lobd(next_boards(bd)) 

    def solve__lobd(lobd): 
     """ 
     Helper function of solve, alongside solve__bd 
     :param lobd: (listof Board) 
     :return:  (listof Board) 
     """ 

     if not lobd: 
      return [] 

     return [solve__bd(lobd[0]), solve__lobd(lobd[1:])] 

    return solve__bd(bd) 


assert solve(B2) == [ 
    [ 
     "X", "X", "O", 
     "O", "X", "O", 
     "X", "X", "X", 
     ], 
    [ 
     "X", "X", "O", 
     "O", "X", "O", 
     "X", "O", "X", 
     ], 
] 


assert solve(B3) == [ 
    [ 
     "X", "O", "X", 
     "O", "O", "X", 
     "X", "X", "X", 
    ], 
    [ 
     "X", "O", "X", 
     "O", "O", "X", 
     "X", "X", "O", 
    ], 
    [ 
     "X", "O", "X", 
     "O", "O", "O", 
     "X", "X", "X", 
    ], 
    [ 
     "X", "O", "X", 
     "O", "O", "O", 
     "X", "X", "O", 
    ], 
] 
+0

例:[B1、B2] – perigon

答えて

0

これは短所のような混乱のようになります。

は、ここに私のpythonのコードです。私はこれをテストしていませんが、私はあなたの問題がここに生じること賭けている:私は推測している

return [solve__bd(lobd[0]), solve__lobd(lobd[1:])] 

あなたの代わりに...

return [solve__bd(lobd[0])] + solve__lobd(lobd[1:])] 

をしたいです。

ただし、Pythonリストはリンクリストではありません。 consは、Racketのリストに要素を追加するための効率的で合理的な方法ですが、1要素リストと長いリストにPythonの+演算子を使用してリストを作成するには、リストの後のすべての要素をコピーする必要があります。

短いリスト(例えば、10K未満の要素のリストに対する線形演算、または100個未満の要素のリストに対する の2次演算)については、それは重要ではありません。 長いものについては、それが行われます。 Pythonの人々は、あなたが間違っていることを伝えます。既存の配列で突然変異を使うべきです。ラケットの人々はPythonの人々が過去に立ち往生しているとあなたに伝えます。素晴らしいプログラミングの世界へようこそ!

+0

@John Clementsにお返事いただきありがとうございます。あなたの解決策は解決策(B2)に役立ちましたが、解決(B3)はまだ失敗しました。私はあなたの提案で次の行を変更しました:return [solve__bd(lobd [0])] + solve__lobd(lobd [1:])しかし、私がB3のために得ている出力は3つのリストで埋められています:[[['X O、X、X、X、X、O、X、O、X、O、X、O、X、O X '、' O '、' O '、' O '、' O '、' O '、' X '、' O ' 'X'、 'X'、 'X'、 'O'、 'O'、 'O'、 'X'、 'X'、 'O'] ]]。私はPythonにラケットのソリューションを移植するとは思わないが、私はこの問題の簡単な解決策を見つけていない。 – bjj

関連する問題