University of Arizona, Department of Computer Science

CSc 120: Word Search

Restrictions

  1. For the long problems, your code should follow the style guidelines for the class. You must following the updated guidelines for commenting classes.
  2. You may not use concepts and/or short-hand syntax not yet covered in class. The restrictions include the following:

Background

Word search is a word game that involves searching for words in a (random) grid of letters. This program simulates the game by searching for words in a grid. The program differs from the physical game in several ways:

Definitions

Given a grid of letters G and a list of words L, a word in G is legal if it meets the following criteria:

File Names

Your program should be in a file named word_search.py. (NOTE: use an underscore, not a dash.)

Expected Behavior

Write a program, in a file named word_search.py, to do the following:

Examples

The following is an example of the grid of letters file:
    y c o d e j
    h s e y p k
    l p h b w a
    l o b w x z
    w o b a a i
    p l y y c g
In this example (and as your program can figure out after reading the first line), N = 6. For this grid, the words your program should print out are:

Input files

You can use the file WORDS, which is a list of about 45,000 words, to test your program. However, note that we may also use other word-lists, which may be bigger or smaller than this list, when testing your code. You should test your code using your own word-lists, which can be bigger or smaller than this list and whose words that may or may not be real English words.

Output format

The words you find should be printed in alphabetical order, one to a line without any extra whitespace.

Note: If the grid-of-letters 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

  1. 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:
    y c o d e j
    Notice that this row contains the words cod, code, and ode. Suppose that the row is represented as the list [‘y’, ‘c’, ‘o’, ‘d’, ‘e’, ‘j’]. A simple way to explore all the possible words (going L to R) in this list would be as follows (the process for the other rows is similar).

    Next consider the problem of searching for words horizontally going right to left. Suppose we want to search the row ​ y c o d e j​ going right to left. This is actually the same as reversing the row, to

    j e d o c y
    and then searching left to right (a problem you’ve solved already). The key thing to note here is that you’ve taken the problem of searching R-to-L and converted it into an equivalent problem involving an L-to-R search, for which you’ve already written code.
  2. Searching vertically. Next consider the problem of searching for words vertically, i.e., among columns. Can you use the function column2list() from the Short Problems for this assignment to solve this problem going from top to bottom? Can you figure out a way of using column2list() with list-reversing to solve the problem of searching vertically going from bottom to top?
  3. Searching diagonally. Searching diagonally is the hardest part of this problem, however, we are only considering one case: upper-left to lower-right.

Program Structure

There are many ways to structure a given program, but at the very least, your program is required to have a main() function that calls the functions that you have written to satisfy the Expected Behavior described previously. Specifically, your program should call functions to read the word-list file and the grid of letters file, call one or more functions that find the legal words in the grid, and, finally, call a function to print out the resulting words found.

The use of a main() function and supporting functions described above is similar to the structure that was provided to you for the word_grid.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 outline below:

def get_word_list():
    ....
    return a list of valid words 

def read_letters_files():
    ...
    return a grid of letters
...
more functions are defined here
...

def main():
    word_list = get_word_list()
    letters_grid = read_letters_file() 

    # a list used to accumulate the valid words found
    all_words = []  

    call a function that finds the words appearing horizontally, passing in the needed arguments

    call a function that finds the words appearing vertically, passing in the needed arguments

    call a function that finds words appearing on the diagonals, passing in the needed arguments

    call a function that prints the words found, passing in the needed argument(s)

 main()  

Be sure to follow the style guidelines for commenting your program.

Testing

Make sure you test your code thoroughly before you turn it in. Some of the things you may wish to consider during testing are: case-sensitivity; different-sized word lists and/or grids; words occurring in different directions in the grid; and words and grids of different size. The test cases used in the Gradescope autograder are given in the assg01-long.zip file on the Assignments webpage.