This assignment involves writing a program to solve word search puzzles. For example in the following word grid it is possible to find the words active, stock, ethernet and java. Note that this exercise involves only horizontal and vertical words; words that occur diagonally (such as python) are not considered as being present in the grid.
1 Inputting the Grid from a File
In the file ex2.py write code to prompt the user for a filename, and attempt to open the file whose name is supplied. If the file cannot be opened the user should be asked to supply another filename; this should continue until a file has been successfully opened.
The file will contain on each line a row from the grid. For example the first two lines of a file that contains the grid shown on the previous page would be
Write code to read, in turn, each line of the file, remove the newline character and append the resulting string to a list of strings.
After the input is complete the grid should be displayed on the screen.
Note that grids used for testing the submitted programs may have different widths and heights from the sample one that will be provided. Grids may be assumed to contain only lower-case letters and all rows in a grid will have the same length.
2 The Word-Search Function
Write a function that will take two arguments: a word to be searched for and a list of strings containing the grid.
The function should searchfor a horizontal (left-to-right or right-to-left) or vertical (downwards or upwards) occurrence of the word (e.g. activex and ethernet in the sample grid). If a word occurs more than once the function is required to find only one occurrence, so you should stop searching once the word has been found.
[ It is recommended that you first write a function that just searches for horizontal occurrences and then write the code for section 2.3 to allow the function to be tested before adding code to the function to search for vertical occurrences. ]
If the word has not been found None should be returned; if the word has been found the function should return a tuple, containing details of where the word is located. The tuple should contain either the coordinates of the first and last letters of the word (i.e. four integers) or the coordinates of the first letter and a direction (i.e. two integers and a string).
3 The Word-Search
After displaying the grid, the program should ask the user to supply a name of a file containing words to be searched for, one word per line. This file should be opened, once again asking the user to supply another name if necessary. You should supply each word in this file in turn to the word-search function. If the function returns None the word should be added to a set of words that have not been found; otherwise the tuple returned should be added as a value in a dictionary using the word as a key.
Aftersearching for all of the words you should display details of all words in the dictionary (in alphabetical order) using a format such as
active found: row 2 col 2 to row 2 col 7
active found: left to right in row 2 starting at col 2
Finally you should output a list of the words that were not found (also in alphabetical order).