University of Arizona, Department of Computer Science

CSc 120: Word Search

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.

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 one to a line without any extra whitespace. The order in which they are printed does not matter.

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. Write a function ​occurs_in( s, L ) that indicates whether the string s occurs in a list of words L ignoring upper/lower case differences.
  2. 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.
  3. 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?
  4. Searching diagonally. Searching diagonally is the hardest part of this problem, however, we are only considering one case: upper-left to lower-right.

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.