Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

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

Word Search

HGAMONIHRA
AOMOKAWONS
NFROLBOBDN
ARFSIHCAGE
LNIEEWONOK
GOLFUNDTHC
KOCATAOHBI
AMRERCGANH
SLGFAMALLC
ALLIGATORX

Words to search

CAT
DOG
ALLIGATOR
CHICKEN
FROG
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...