I've made a function to search horizontally for a word in a matrix full of letters. Now I'm trying to reuse this code in order to find any word in matrix. I need to read the matrix line by line, compare the words made with my list of words (like a dictionary) and if the word exists in the list, a word has been found. This is my initial code:
def search(p):
x=matrix() #matrix of letters
for line in x:
if p in ''.join(line):
return True
I've written some other code but none of it works. I've been looking for similar questions but none of them answer my question.
This is the code I have and doesn't work:
def auto_search():
l=[] #list of words found
x=matrix()
for line in x:
for i in ''.join(line):
if search_dic(i)!=-1: #searchs in the list of words if the word is there
l.append(i)
print (l)
For example, a have this matrix:
[[a,p,e,n],
[g,h,j,k],
[e,r,l,d]]
The function has to read the matrix and find the word "pen" by itself. Any help would be appreciated. Thank you
See Question&Answers more detail:os