解决单词搜索并删除找到的字符 [英] Solving a wordsearch and delete the found characters
问题描述
这是问题所在
要解决这个难题,您需要搜索,然后从单词搜索中删除列表中所有出现的单词(如果有多个).
To solve the puzzle you need to search and then delete from the wordsearch all the OCCURRENCES (if multiple) of the words in the list.
将保留的图表字母,按行和列顺序排列, 它们将构成游戏的解决方案.
The letters of the diagram that will remain, taken all in their order by rows and by columns, they will form the solution of the game.
单词可以在图表中水平出现(从右到左, 或从左至右),垂直(向下或向下朝 顶部)和对角线(从顶部到底部或从底部到顶部).
Words can appear in the diagram horizontally (from right to left, or from left to right), vertically (downwards or downwards towards the top) and diagonally (from top to bottom or from bottom to top).
定义一个函数es1(ftxt),该函数使用文本文件的地址, 包含详细信息的单词图并返回字符串 游戏的解决方案.
Define a function es1 (ftxt), which takes the address of a text file, contains a word diagram of a crucipuzzle and returns the string solution of the game.
fname文件包含单词列表后面的单词搜索. 图之前的一系列1条或多条空行将图与列表分开 单词并遵循单词列表. 该图按行记录(每行一行和连续行) 每行的制表符由单个字符('\ t')分隔. 连续繁忙单词的列表,每行一个单词.
The fname file contains the wordsearch following the word list. A series of 1 or more blank lines preceding the diagram separates the diagram from the list of words and follows the word list. The diagram is recorded by lines (one line per line and consecutive lines) tab of each row are separated by a single character ('\ t'). The list of consecutive busy words, one word for each line.
O T N E G R A S A E
R N N C O R A L L O
O A I B L U E E V G
U T O R E N T I I A
V I O L E T T O O R
O C R A R I A E L O
D A B I M A L V A P
I P C I E L O G L R
C O R P O S O U A O
A P I E N O M I L P
ACIDO
ARGENTO
BLU
CIELO
CORALLO
CORPOSO
ELETTRICO
LATTE
LIMONE
MALVA
NERO
OCRA
OPACITA
ORO
PAGLIERINO
PIENO
PORPORA
PRIMITIVO
VIOLA
VIOLETTO
我找到了所有的行,列和对角线的50%, 但是我不知道如何找到各个方向上找到的字符的坐标以将其删除,然后找到解决方法.
I have found all the rows, columns and 50% of the diagonals, but I don't know how to find the coordinates of characters found in all directions to delete it and then have the solution.
这是我的代码:
with open('cp5_Colori.txt', 'r') as f:
data=f.read().replace("\t","")
data=data.split("\n\n")
lista_parole=data[1].split()
lista_orizzontale=data[0].split()
oriz_contraria=[x[::-1] for x in lista_orizzontale]
diz={}
c=0
b=0
cruzi_verticali=[]
for x in lista_parole: #loop to find rows and add the
found
words to a diz
for y in lista_orizzontale:
if x in y:
diz[x]=1
for z in oriz_contraria:
if x in z:
diz[x]=1
while c <= len(lista_orizzontale):
cruzi_verticali.append(lista_orizzontale[c][b]) #loop for
columns
c+=1
if c==len(lista_orizzontale):
cruzi_verticali.append("///")
c=0
b+=1
if b==len(lista_orizzontale):
c=len(lista_orizzontale)+1
joinata="".join(cruzi_verticali)
parole_verticali=joinata.split("///")
vert_contraria=[k[::-1] for k in parole_verticali] #convert to a list
of
strings and find
the
reversed of
colums
conta=0
conta2=0
for x in lista_parole:
for y in parole_verticali:
if x in y: #loop to add search word to
the diz
diz[x]=1
for z in vert_contraria:
if x in z:
diz[x]=1
cruzi_diagonali=[]
parole_diagonali=[]
diag_contraria=[]
prova=[]
itera=len(parole_verticali)**2
while len(prova)!=len(parole_verticali)-1:
cruzi_diagonali.append(parole_verticali[conta][conta2])
conta+=1
conta2+=1
if conta==len(lista_orizzontale):
cruzi_diagonali.append("///")
#loop to find a part of
diagonals
if conta==len(parole_verticali)-1:
conta=0
if conta==0:
prova.append(0)
conta=conta+len(prova)
conta2=0
prova2=[]
conta3=0
conta4=1
while len(prova2)!=len(parole_verticali)-1:
cruzi_diagonali.append(parole_verticali[conta3][conta4])
conta3+=1
conta4+=1
if conta4==len(lista_orizzontale):
cruzi_diagonali.append("///")
#loop to find lower
diagonals
if conta4==len(parole_verticali)-1:
conta4=0
if conta4==0:
prova2.append(0)
conta4=conta4+len(prova2)
conta3=0
joinata2="".join(cruzi_diagonali)
parole_diagonali=joinata2.split("///") #convert diagonals
into
a list of strings
diag_contraria=[k[::-1] for k in parole_diagonali]
for x in lista_parole:
for y in set(parole_diagonali):
if x in y: #loop to add the found words in
the dictionary as keys
diz[x]=1
for z in set(diag_contraria):
if x in z:
diz[x]=1
soluzione=[]
lista_totale=[]
lista_orizzontale2=lista_orizzontale[:]
for k in diz.keys():
for k2 in lista_orizzontale2: #all the found words in
the
row replaced with "*"
if k in k2:
hg=len(k)*"*"
k3=k2.replace(k,hg)
lista_orizzontale2.append(k3)
if "*" not in k2:
lista_orizzontale2.remove(k2)
有人可以通过在单词搜索中找到找到的字母的所有坐标来帮助我吗?
Could someone help me by finding all the coordinates of the found letters in the wordsearch?
推荐答案
您正在使自己变得困难.无需按每个方向变换拼图阵列,只需依次访问每个字母并从该字母开始的八个方向中的每个方向寻找单词.
You're making things rather difficult for yourself. Instead of transforming the puzzle array in every direction, just visit each letter in turn and look for words in each of the eight directions starting from this letter.
由于这似乎是一种家庭作业,因此我将留给您填写详细信息,但是代码的基本轮廓应如下所示:
Since this appears to be some sort of homework, I'll leave you to fill in the details, but the basic outline of your code should look something like this:
puzzle = [list(row) for row in 'OTNEGRASAE', 'RNNCORALLO', 'OAIBLUEEVG',
'UTORENTIIA', 'VIOLETTOOR', 'OCRARIAELO', 'DABIMALVAP', 'IPCIELOGLR',
'CORPOSOUAO', 'APIENOMILP']
word_list = ['ACIDO', 'ARGENTO', 'BLU', 'CIELO', 'CORALLO', 'CORPOSO', 'ELETTRICO',
'LATTE', 'LIMONE', 'MALVA', 'NERO', 'OCRA', 'OPACITA', 'ORO', 'PAGLIERINO',
'PIENO', 'PORPORA', 'PRIMITIVO', 'VIOLA', 'VIOLETTO']
for {each word in word_list}:
for {each cell in puzzle}:
if {cell.upper() == first character of word}:
for direction in [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]:
(dx, dy) = direction
{Does the puzzle contain all the other characters of word in this direction?}
{If so change these cells to lower case, and skip to next word}
{extract all the remaining upper case letters from the puzzle}
这篇关于解决单词搜索并删除找到的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!