Python单词搜索(最简单)

2024-04-19 01:28:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为python创建一个单词搜索代码,但我遇到了一个死胡同。我能在给定的黑板上找到单词的起始字母。但我不能让它朝着正确的方向走,它只是字面上找到了每个字母,我知道我仍然缺少几个块,但我似乎无法继续工作代码是:

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')

这将产生以下输出:

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

我能做些什么来检查后面的字母,我试着把这个放进去(嵌套在每个elif块下面):

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')

当然,每个方向的增量各不相同,但它会导致索引器:列表索引超出范围。当行数2达到2时,循环仍然会运行,即使行长的值只有2,为什么循环会继续?有什么想法吗?还有,我走对了吗?你知道吗


Tags: andrightnumbercolumnleftlengthwordrows
1条回答
网友
1楼 · 发布于 2024-04-19 01:28:39

我建议用rows列表做一个矩阵。然而,你可以选择把它们作为一个完整的词。我想两种方法都可以。你知道吗

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

我用$包围了matrix,以便更容易检测到行尾对角线。这大大简化了末端检测。你知道吗

我的解决方案有4个函数,main, build_matrix, find_word, walk。你知道吗

我不认为发布我的解决方案是好的,但是我会发布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

相关问题 更多 >