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
Solution 1:
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.
Post a Comment for "In Python Word Search, Searching Diagonally, Printing Result Of Where Word Starts And Ends"