在Python单词搜索中,对角搜索,打印单词开始和结束的结果 [英] In Python word search, searching diagonally, printing result of where word starts and ends

查看:52
本文介绍了在Python单词搜索中,对角搜索,打印单词开始和结束的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个朋友辅导我学习 Python,他给了我这个项目,用户将在该项目中将单词搜索读入程序,并且该文件包含将在单词搜索中出现的单词列表.我必须搜索这些词,其中一些词对角线.如果我找到了这个词,我必须打印出这个词开始和结束的行和列(坐标).我只学习了 2 个星期的 Python,所以我很困惑,我如何对角搜索单词并获得单词的起点和终点?示例单词搜索在下方,要搜索的单词随附.我已经解决了它并花了 3 天的时间,但没有任何结果.

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. 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? 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.

词搜索

HGAMONIHRA
AOMOKAWONS
NFROLBOBDN
ARFSIHCAGE
LNIEEWONOK
GOLFUNDTHC
KOCATAOHBI
AMRERCGANH
SLGFAMALLC
ALLIGATORX

要搜索的词

CAT
DOG
ALLIGATOR
CHICKEN
FROG

推荐答案

这更像是一个蛮力问题,但是有更有效的技术可用,但请记住,您是该领域的新手,我不会建议您专注于算法部分,所以首先我们将创建一个名为 search_diagonal 的函数,它将采用 3 个参数作为 starting_point, mesh, >length_of_word 并且您可以根据传递的参数在该函数中做一些漂亮的事情.

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.

一个你有 3 个参数,然后你可以很容易地按对角线传播:

One you have 3 arguments you can then easily propagate diagonally as:

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.

这篇关于在Python单词搜索中,对角搜索,打印单词开始和结束的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆