Question: Expected Behavior Wite a program, in a fie namad word _ search. py , to do the folowing: the word - lst fie cortains ond
Expected Behavior
Wite a program, in a fie namad wordsearch. py to do the folowing:
the wordlst fie cortains ond word per line,
Examples
The following is an axample of the grid of lettera fie:
::
In tris example and as your program can figure out atter reading the first line For this gid, the worda your program should print out are:
: bode, cod, odo, lob prorizomal, LtoR
bow, yes, doc horizomal RtoL
: spool pool, way vertical toptobottom
locp, lopa vertical botomtokop
lob, wag diagoral toplati to bottomright
Input files
Output format
The worda you find ahould be printed in alphabatical ordar, one to a line without ary axtra whitespace.
Note: If the gidotlatters fie is empty, these is no output.
Development Strategy
Data Structures
Organize the list of valid words as a lats of stringe. Organize the grid as a list of ists.
Program development
Seaching harizontally First, consider the problem of finding words horiaontaly, in the grid going from left to right. Coraider the frst row in the example shown above
ycodej
Neat consider the problem of soarching for worda horizontally going right to left. Suppose we want to search the row y o d e going right to lett. This is actually the aame as reversing the row, to
iedocy
If you can entract each diagonal into a list of latiors, then you can simply saarch through this ist as you did boforeagain, reducing this probliam to one youlve already writien coda to solve.
Program Structure
The use of a naia function and supporing functions doscribed above is similar to the structure that was provided to you for the wardgrid. py program
Note: You may not use global variablea.
If you would lixe more guidance on how to structure your program, you may use the outline below:
def getwordList:
retera a list of valid words
det readletterstiles:
reture a grid of letters
wore functions are defined here
naia:
werdist getwordtist
lettersgrid readliettersile
call a function that finds the words appearing horizontalty, passing in the needed argunents
call a function that finds the words appearing vertically, passing in the needed arguneents
call a function that finds words appearing on the diagonals, passing in the needed argunents
call a function that prints the words found, passing in the needed argueents
maia
Examples
The following is an example of the grid of letters file:
In this example and as your program can figure out after reading the first line For this grid, the words your program should print out are:
code, cod, ode, lob horizontal LtoR
bow, yes, doc honizontal RtoL
spool, pool, way vertical toptobottom
: loop, loops vertical bottomtotop
Input files
Output format
The words you find should be printed in alphabetical order, one to a line without any extra whitespace.
Note: If the gridotletters file is empty, there is no output.
Development Strategy
Data Structures
Organize the list of valid words as a list of strings. Organize the grid as a list of lists.
Program development
Searching horizontally. First, consider the problem of finding words horizontally in the grid going from left to right. Consider the first row in the example shown above:
ycodej
Next consider the problem of searching for words horizontally going right to left. Suppose we want to search the row y @ e j going right to left. This is actually the same as reversing the row, to
jedocy
If you can extract each diagonal into a list of letters, then you can simply search through this list as you did beforeagain, reducing this problem to one you've already written code to solve.
Do you see a pattern in how the and coordinates change between successive elements? Can you use this pattern to extract a diagonal going from the upperleft to lowerright given its starting coordinates into a list?
Program Structure
The use of a main function and supporting functions described above is similar to the structure that was provided to you for the wordgrid. py program.
Note: You may not use global variables.
If you would like more guidance on how to structure your program, you may use the outine below:
def getwordlist:
return a list of valid words
def readlettersfiles:
return a grid of letters
more functions are defined here
ef main:
wordlist getwordlist
lettersgrid read lett
# a list used to accumulate the valid words found
allwords
call a function that finds the words appearing horizontally, passing
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
