我正在创建一个单词搜索求解器,需要一种方法来旋转列表中的单词搜索,所以左下角是“顶部”,右下角是“底部”
我有这个:
Puzzle = ["FUNCTIONRRIRAI",
"RAIOONFRCCPWON",
"PTCSNOBEUITOLO",
"BNCACIANTOSLIH",
"RBYOLILYNREFBT",
"HYYNOGESTIBRIY",
"AATTSIONCMCENP",
"UORTENRRCBFVAU",
"CEBEECVWIERORI",
"PROCESSORTOPYF",
"OHCOMPUTERHSOS",
"YCYPRESREOSMRW",
"OATHBRMVTHHCTR",
"PGORWOOUIPSCHP"]
我需要它的形成:
Puzzle = ["F","RU","PAN","BTIC",...]
所以看起来这个词搜索已经旋转了45度
任何建议/帮助将不胜感激
find_horizontal 和要查找的单词的代码:
def load_words_to_find(file_name):
word_list = []
file = open(file_name, "r")
for line in file.readlines():
word_list.append(line)
word_list = list(map(lambda s: s.strip(), word_list))
return word_list
def find_horizontal(Puzzle, Words, ReplaceWith, Found):
# Parameters :- List:Puzzle, List:Words, Character:ReplaceWith, List:Found
# Return :- List:Outpuz, List:Found
# Find all words which are horizontally in place (left to right and right to left), return the puzzle and list of found words
rev = ''
Outpuz = Puzzle
for line in Puzzle:
rev = line[::-1]
for word in Words:
if word in line:
Found.append(word)
Puzzle[Puzzle.index(line)] = line.replace(word, ReplaceWith * len(word))
if word in rev:
Found.append(word)
Puzzle[Puzzle.index(line)] = line.replace(word[::-1], ReplaceWith * len(word))
else:
pass
print("Found: ", Found)
print(Outpuz)
return Outpuz, Found
find_horizontal(Puzzle, load_words_to_find("words.txt"), ".", [])