简体   繁体   中英

Python Word Search (simplest)

I am creating a word search code for python and I hit a dead end. I was able to find the starting letters of the words in the given board. But I can't make it go to the right direction it just literally finds every letter, I know I am still missing a few blocks but I can't seem to how to proceed the working code is:

words=['row', 'war', 'raw','rar','dew','rod','red']
rows= ['rar', 'aoe', 'wed',]#rows in the board
print(rows[-2][1])
row_length=len(rows[0])-1
column_length=len(rows)-1
print(row_length,column_length)
coordinates=[]

for row_number, row in enumerate(rows):
    for column_number, letter in enumerate(row):
        for word in words:
                if word[0]==letter:
                     if row_number <= 0 and rows[row_number +1][column_number] == word[1]:

                         print(word,'down')
                     elif row_number >= 0 and rows[row_number-1][column_number] == word[1]:
                         #up
                         print(word,'up')
                     elif row_number >= 0 and column_number <= 0 and rows[row_number] 
                  [column_number+1] == word[1]:
                         #down#right
                         print(word,'right')
                     elif row_number >= 0 and column_number <= 0 and rows[row_number -1] 
                 [column_number +1] == word[1]:#up-right
                         print(word,'up-right')
                     elif row_number >= 0 and column_number >= 0 and rows[row_number +1] 
                 [column_number -1] == word[1]:
                         #down#down-right
                         print(word,'down-right')
                     elif row_number >= 0 and rows[row_number][column_number-1] == word[1]:
                         #down#left
                         print(word,'left')
                     elif row_number >= 0 and column_number >= 0 and rows[row_number -1] 
                 [column_number -1] == word[1]:
                         #down#up-left  
                         print(word,'up left')
                     elif row_number >= 0 and column_number >= 0 and rows[row_number+1] 
                 [column_number -1] == word[1]:
                        #down#down-left
                         print(word,'down left')

This would give an output of:

raw down
rar down
red up-right
row down-right
raw left
rar left
rod down-right
red down
war up
dew up

What could I do to check the succeeding letters, I tried putting this in(nested below each elif block):

row_number2=row_number
while row_number2 <= row_length and column_number <= column_length and rows[row_number2][column_number] != word[-1]:
         row_number2+=1
         if rows[row_number2][column_number]==word[-1]:
         print(word,'down')

of course each increment each different per direction but it results to an IndexError: list index out of range. The loop would still run when the row_number2 reaches 2, even though the row_length's value is just 2 why does the loop keep going on? Any ideas? Also, am I in the right track?

I would suggest making a matrix out of the rows list. However, you may choose to keep them as whole words. I imagine either way would do.

>>> pprint(matrix)
[['$', '$', '$', '$', '$'],
 ['$', 'r', 'a', 'r', '$'],
 ['$', 'a', 'o', 'e', '$'],
 ['$', 'w', 'e', 'd', '$'],
 ['$', '$', '$', '$', '$']]

I surrounded the matrix with $ to make it easier to detect end of row, diagonal. This greatly simplifies detecting the end.

My solution had 4 functions, main, build_matrix, find_word, walk .

I don't think it would be good to post my solution, but I'll post walk .

def walk(matrix, word, idx, dir_):
    x,y = idx # start pos
    r,c = idx # will contain end pos
    i = 0
    while True:
        # either the 2 letters don't match or run into the border ($)
        if matrix[r][c] != word[i]:
            return False;
        if i == len(word)-1:
            return ((x-1, y-1), (r-1, c-1)) # 'var'-1 because of '$' border

        if dir_ == 'n':
            r -= 1
        elif dir_ == 'ne':
            r -= 1
            c += 1
        elif dir_ == 'e':
            c += 1
        elif dir_ == 'se':
            r += 1
            c += 1
        elif dir_ == 's':
            r += 1
        elif dir_ == 'sw':
            r += 1
            c -= 1
        elif dir_ == 'w':
            c -= 1
        elif dir_ == 'nw':
            r -= 1
            c -= 1

        i += 1


def find_word(matrix, word, indices, heading):
    for  idx in indices: # indices may contain more than 1 pair
        for dir_ in heading: # try up to 8 headings
            tpl = walk(matrix, word, idx, dir_) # search the matrix
            if tpl != False:
                return tpl
    return False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM