[英]get all words combinations and path from letters arry
我創建了一個 boogle 游戲,我需要構建一個接收輸入的函數:字母板(列表列表)、合法單詞列表和整數 n。
該函數必須返回所有 n 長度的有效單詞軌道。 例如 n = 3 那么該函數必須返回三長板上的所有路徑,這些路徑實際上是有效的單詞。
我編寫了一個代碼,在特定示例中返回必須返回的三個路由中的一個路由。
輸入:
board1 = [['Q', 'O', 'Q', 'Q'],
['D', 'O', 'G', 'Q'],
['Q', 'O', 'Q', 'Q'],
['Q', 'Q', 'Q', 'Q']]
word_dict = {'DOG': True}
n = 3
board = Board(board1)
length_n_paths(3, board, word_dict)
我的輸出:
[((1, 0), (1, 1), (1, 2))]
想要的輸出:
[[(1, 0), (0, 1), (1, 2)], [(1, 0), (1, 1), (1, 2)], [(1, 0), (2, 1), (1, 2)]]
我使用了一個組合,首先找到長度為n的字母的所有可能組合,然后我通過一個坐標坐標,根據它前面的坐標檢查每個坐標是否在有效位置,然后我檢查是否來自字母組合的單詞是單詞列表中的單詞。 如果是這樣 - 我將在列表中返回它的路徑以及其他合法單詞路徑。
我的代碼:
direct_lst=['Up','Down','Right','Left','Up_right','Up_left','Down_right','Down_left']
class Board:
def __init__(self, board):
self.board = board
def get_board_coordinate(self):
cord_lst = []
row = len(self.board)
col = len(self.board[0])
for i in range(row):
for j in range(col):
cord_lst.append((i, j))
return cord_lst
def possible_directions(self, coordinate, next_coordinate):
y, x = coordinate
directions_funcs = {
# A dictionary that matches between a letter and the direction of the desired search
'Up': (y - 1, x),
'Down': (y + 1, x),
'Right': (y, x + 1),
'Left': (y, x - 1),
'Up_right': (y - 1, x + 1),
'Up_left': (y - 1, x - 1),
'Down_right': (y + 1, x + 1),
'Down_left': (y + 1, x + 1)
}
it_ok = False
for direction in direct_lst:
if directions_funcs[direction] == next_coordinate:
it_ok = True
return it_ok
def is_valid_path(board, path, words):
word = board.board[path[0][0]][path[0][1]]
board_coordinates = board.get_board_coordinate()
for cord in range(len(path)-1):
if path[cord] in board_coordinates and path[cord+1] in board_coordinates:
if not board.possible_directions(path[cord], path[cord + 1]):
return None
else:
word += board.board[path[cord + 1][0]][path[cord + 1][1]]
else:
return None
if word in set(words):
return word
import itertools
def create_dict(board, n):
new_dict = dict()
row = len(board.board)
col = len(board.board[0])
for i in range(row):
for j in range(col):
new_dict[(i, j)] = board.board[i][j]
result_list = list(map(list, itertools.combinations(new_dict.items(), n)))
return result_list
def coordinates_lst_and_str_lst(board, n):
combine = create_dict(board, n)
all_cord_dic = dict()
for lst in combine:
is_it_ok = True
cord_lst = []
str_l = ""
for i in range(n):
cord_lst.append(lst[i][0])
str_l += lst[i][1]
try:
if not board.possible_directions(lst[i][0], lst[i + 1][0]):
is_it_ok = False
break
except IndexError:
break
if is_it_ok:
all_cord_dic[tuple(cord_lst)] = str_l
all_cord_dic[tuple(cord_lst)[::-1]] = str_l[::-1]
return all_cord_dic
def length_n_paths(n, board, words):
possible_words = coordinates_lst_and_str_lst(board, n)
my_dict = {key:val for key, val in possible_words.items() if val in words}
return list(my_dict.keys())
我認為問題出在組合中,但我不知道如何解決。 我很樂意提供任何幫助。
調試后,很明顯結果possible_words
不包含鍵(1, 0), (0, 1), (1, 2)
,所以這就解釋了為什么它不是答案的一部分——所以問題變成了為什么不t 調用coordinates_lst_and_str_lst()
生成該元組(以及另一個“缺失”的元組)
如果在coordinates_lst_and_str_lst
_lst_and_str_lst 中構造combine
后中斷,您會發現[((1, 0), 'D'), ((0, 1), 'O'), ((1, 2), 'G')]
不在combine
中,這意味着coordinates_lst_and_str_lst
找不到它作為解決方案。
因此,問題一定出在create_dict
中,顯然它並沒有創建所有合法動作。
實際上,在create_dict
中,您使用itertools.combinations()
,它為您提供集合中n
項的所有唯一組合,而不管它們的 order ,但您關心的是 order。
所以,你不想要itertools.combinations(new_dict.items(), n)
,你想要itertools.permutations(new_dict.items(), n)
。 仔細看看組合和排列(大小為 n)之間的區別。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.