Python Forum
Search character from 2d list to 2d list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search character from 2d list to 2d list
#1
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.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.
Reply
#4
The letter on the board is board[row_n][col_n].
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to reverse a list and store in another list in python SuperNinja3I3 6 3,293 Aug-14-2022, 06:36 PM
Last Post: DeaD_EyE
Question Python - List - Int List sophi 8 2,542 Apr-21-2022, 07:55 PM
Last Post: sophi
  Change each character of list into an different characters Angry_bird89 1 2,056 Jun-19-2020, 08:29 AM
Last Post: Angry_bird89
  Check if a list exists in given list of lists Daniel94 2 2,236 Apr-07-2020, 04:54 PM
Last Post: deanhystad
  list of strings to list of float undoredo 3 2,691 Feb-19-2020, 08:51 AM
Last Post: undoredo
  arrays sum list unsupported operand type(s) for +=: 'int' and 'list' DariusCG 7 4,166 Oct-20-2019, 06:24 PM
Last Post: Larz60+
  have homework to add a list to a list using append. celtickodiak 2 2,011 Oct-11-2019, 12:35 PM
Last Post: ibreeden
  I donr know how to search from 2d list to 2d list AHK2019 1 1,762 Sep-25-2019, 01:59 AM
Last Post: ichabod801
  sort a list alphabeticaly without changing the original list Holmen 5 4,206 Jan-27-2019, 01:49 PM
Last Post: Holmen
  Remove special character from list vestkok 3 4,226 Nov-04-2018, 01:48 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020