Python Forum

Full Version: Search character from 2d list to 2d list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is basically a word search solver. I tried to solve the words that must be searched, (list_2), in the matrix board where each character from each word must be sought from.
def search_word(words, board):
    sol =()
    solution = ""
    dir_x = { 1 : "right",
             -1 : "left"}
    dir_y ={1: "down",
            -1: "up"}
    for row_n, row in enumerate(board):
        for col_n, letter in enumerate(row):
            for word in words:
                for i in range(len(word)):
                    if word[0]==letter:
                        if word[0+i] == (col_n + i).values:
                            solution = (col_n, row_n)
                            return sol +solution, "%s" %(dir_x.value(1))#horizontal right 
                        elif word[0+i] == (col_n-i).values:
                            solution = (col_n, row_n)
                            return sol+solution, "%s" %(dir_x.value(-1))
                             #horizontal left
                        elif word[0+i] == (row_n+i).values.values:
                            solution = (col_n, row)
                            return sol+solution, "%s" %(dir_y.value(1)) 
                        #vertical down
                        elif word[0+i] == (row_n-i).values.values:
                            solution = (col_n, row)
                            return sol+solution, "%s" %(dir_y.value(-1)) 
                            #vertical up
                        elif word[0+i] == ((row_n+i).values+i).values:
                            solution = (col_n, row)
                            return sol+solution, "%s- %s" %(dir_x.value(1),dir_y.value(1))
                            #down-right
                        elif word[0+i] == ((row_n-i).values+ i).values:
                            solution = (col_n, row)
                            return sol+solution, "%s- %s" %(dir_x.value(1),dir_y.value(-1))
                            # up-right
                        elif word[0+i] == ((row_n+i).values -i).values:
                            solution = (col_n, row)
                            return sol+solution, "%s- %s" %(dir_x.value(-1),dir_y.value(1))
                            # down- left
                        elif word[0+i] == ((row_n -i ).values -i).values:
                            solution = (col_n, row)
                            return sol+solution, "%s- %s" %(dir_x.value(-1),dir_y.value(-1))
                            # up-left 
                        else:
                            print ("Out of bounds")


list_1= ["rar",
         "war",
         "raw",
         "rar",
         "dew",
         "rod" ,
         "red" ]
list_2 =["rar", "aoe", "wed"]
answer = search_word(list_2, list_1)
print (answer)


that is my code and the error I get comes from the if else statements where the col_n has no values( i enumerated the list of that contains the list of board to have columns and rows). I am not sure on how to fix this. Please help.
I'm really confused here. It looks like row_n is an integer and i is an integer. What then is (row_n + i).values supposed to be (much less (row_n + i).values.values)? That just gives me an error.

I think you are over complicating this. Assuming everything is in straight lines, there are eight directions you can go in. Go through each character in the grid, setting it as the start. Loop through the eight directions, get the characters in that direction from the starting character, see if they match any of the words.

Set the directions up as coordinate changes like (0, + 1) or (+1, -1). That way you can have one procedure for going in a given direction, rather than a big if/elif chain.
(Sep-25-2019, 05:35 PM)ichabod801 Wrote: [ -> ]I'm really confused here. It looks like row_n is an integer and i is an integer. What then is (row_n + i).values supposed to be (much less (row_n + i).values.values)? That just gives me an error. I think you are over complicating this. Assuming everything is in straight lines, there are eight directions you can go in. Go through each character in the grid, setting it as the start. Loop through the eight directions, get the characters in that direction from the starting character, see if they match any of the words. Set the directions up as coordinate changes like (0, + 1) or (+1, -1). That way you can have one procedure for going in a given direction, rather than a big if/elif chain.

the way i saw the col_n.values.values is that the index of col_n, gets the index of row_n which in turns returns the value, the letter in the board. I will work through it in more detailed manner. Thank you for the criticism, will post what I have done later.
The letter on the board is board[row_n][col_n].