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
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 directionnew_word1+=MESH[starting_point[0]+i][starting_point[1]+i]for i in xrange(length_of_word):#Propagating in NE directionnew_word2+=MESH[starting_point[0]+i][starting_point[1]-i]for i in xrange(length_of_word):#Propagating in NW directionnew_word3+=MESH[starting_point[0]-i][starting_point[1]-i]for i in xrange(length_of_word):#Propagating in SW directionnew_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.