简体   繁体   English

在Python单词搜索中,对角搜索,打印单词开始和结束位置的结果

[英]In Python word search, searching diagonally, printing result of where word starts and ends

I have a friend of mine tutoring me in learning Python and he gave me this project where a user will read a word search into the program and the file includes a list of words that will be in the word search. 我有一个朋友教我学习Python,他给了我这个项目,用户将在该项目中读取单词搜索,并且文件中包含将在单词搜索中使用的单词列表。 I have to search for these words and some of the words run diagonally. 我必须搜索这些单词,并且其中一些单词对角排列。 If I find the word, I must print in what row and what column (the coordinates) the word starts and the word ends. 如果找到该单词,则必须在该单词的开始和结束位置的哪一行和哪一列(坐标)中打印。 I've only been learning Python 2 weeks so I'm confused, how would I search for a word diagonally and get the starting point and ending point of a word? 我只学习Python 2个星期,所以我很困惑,如何对角搜索一个单词并获得单词的起点和终点? The sample word search is down below and the words to search are with it. 示例单词搜索位于下方,搜索单词随其而来。 I have worked through it and spent 3 days on it and nothing has come of it. 我已经完成了该过程,并花了3天时间,却一无所获。

Word Search 单词搜索

HGAMONIHRA
AOMOKAWONS
NFROLBOBDN
ARFSIHCAGE
LNIEEWONOK
GOLFUNDTHC
KOCATAOHBI
AMRERCGANH
SLGFAMALLC
ALLIGATORX

Words to search 搜寻字词

CAT
DOG
ALLIGATOR
CHICKEN
FROG

This is more of a brute force problem, however there are more efficient techniques available but keeping in mind that you are new to this field I won't suggest you to focus on algorithmic part, So first of all we will create a function called search_diagonal which will take 3 arguments as starting_point , mesh , length_of_word and you can do some pretty stuff inside that function depending upon the arguments passed. 这更多的是蛮力问题,但是有更多有效的技术可用,但是请记住,您是该领域的新手,我建议您不要专注于算法部分,因此,我们首先将创建一个名为search_diagonal的函数这将使用3个参数作为starting_pointmeshlength_of_word并且您可以根据传递的参数在该函数内做一些漂亮的事情。

One you have 3 arguments you can then easily propagate diagonally as: 其中一个有3个参数,然后可以按对角线轻松传播为:

MESH = ["HGAMONIHRA", "AOMOKAWONS", "NFROLBOBDN", "ARFSIHCAGE", 
"LNIEEWONOK", "GOLFUNDTHC", "KOCATAOHBI", "AMRERCGANH", "SLGFAMALLC", 
"ALLIGATORX"]

def search_diagonal(starting_point, MESH, length_of_word):
    new_word1 = ""
    new_word2 = ""
    new_word3 = ""
    new_word4 = ""
    for i in xrange(length_of_word):
        #Propagating in SE direction
        new_word1+=MESH[starting_point[0]+i][starting_point[1]+i]
    for i in xrange(length_of_word):
        #Propagating in NE direction
        new_word2+=MESH[starting_point[0]+i][starting_point[1]-i]
    for i in xrange(length_of_word):
        #Propagating in NW direction
        new_word3+=MESH[starting_point[0]-i][starting_point[1]-i]
    for i in xrange(length_of_word):
        #Propagating in SW direction
        new_word4+=MESH[starting_point[0]-i][starting_point[1]+i]
    return new_word1, new_word2, new_word3, new_word4

However there is a need to handle a lot of exception cases like index out of range etc. but this must give you a rough idea of how this problem can be solved. 但是,需要处理很多异常情况,例如索引超出范围等。但这必须使您大致了解如何解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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