CSc 110 – WordSearch

WordSearch image

In this assignment, you will be tasked with writing a program that finds certain words within a wordsearch grid and prints out the word mapped to the line and starting character location of each word. The information of the board, and the words will be provided by the user via files you will need to read in. The user will also need to specify the direction of the words: Horizontal or Vertical. You should name the file wordsearch.py.

Program Behavior

This program should accept three inputs from the user. The first input is the name of the file containing the words you will be looking for (See section on words.txt). The second input is the name of the file containing the wordsearch board (See section on puzzle.txt). The third input will be the direction you will be looking in Horizontal or Vertical.

words.txt

Your program should open up and read a file names words.txt. You can assume that this file already exists in the same directory as wordsearch.py. When testing on your computer, you should create your own words.txt. Using this information, you can determine which words you are searching for in your puzzle file! You can assume that each line of words.txt is formatted as follows:

forward_word:backward_word
....

In other words, the first word on a line of the file is a word from the “word bank”. A colon separates that word from the backwards version of that same word. For instance:

computer:retupmoc
science:ecneics

Your program should open and read the words.txt file into the program and store the mapping between forwards and backwards words in a dictionary. The keys of the dictionary should be the forward version of the word, and the value the keys map to, should be the backward version of that word. Given the example content of words.txt shown above, the words dictionary should have the keys and values shown below:

words = { 
    "computer":"retupmoc",
    "science":"ecneics",
}

After you have read in the file and constructed this dictionary, you can use it to determine your word bank, which words you will need to look for within the puzzle.txt.

puzzle.txt

This file contains a grid of letters and hidden within those letters are certain words you will need to be searching for. You will use your newly created words dictionary to figure out what letter combination of words you need to find. This is an example of what this word-grid file would look like:

W O D I W S M Z H 
N R M E W A N D A 
L Z H I N O K L H 
S R E G N E V A V 
I F R C X H C Q X 
A H T Z B A Y K Q 
A R A M E R I C A

The puzzle file could have more or less Rows/Columns depending on each test case.

Output

The program should print out the starting row / col locations for each word found. The formatting of the outut should be something like:

word1 found at [row1, col1]
word2 found at [row2, col2]
...

It should print out the words in the order found. In horizontal mode, this means printing out the words found in the rows from top to bottom. In vertical mode, this means printing out the words found in the columns left to right.

Example Run

Say that words.txt contains:

army : ymra
navy : yvan
star : rats
america : acirema
bullet : tellub

and then grid.txt contains:

Z S Y M R A Z
R N Z Z A Q L
M A T R T U I
N V Y A S S D
B Y Z T C V B
V B U L L E T
A M E R I C A

Then the inputs words.txt, grid.txt and h (horizontal mode) should give these results:

army found at [1, 6]
bullet found at [6, 2]
america found at [7, 1]

The inputs words.txt, grid.txt and v (vertical mode) should print this instead:

navy found at [2, 2]
star found at [4, 5]

Development Strategy

This section covers how you should go about structuring and implementing the code. In total, your program should have at least 5 functions. My recommendation for these five functions:

You may have other functions as well. You should build this program in the following sequence:

(1) Create a main function

In this step, create the main function, and plan to call some of your other functions from main. The main function should ask the user for the three inputs, call a function to load in the contents of the dictionary file, determine the mode, and then call the correct function for handling the wordsearch depending on the selected mode.

(2) Reading in words.txt

Create a function that will be responsible for opening up the words file and reading in the information. Main should have already asked the user for the name of the file. You can pass the file name into this function via arguments / parameters. The function should iterate through the lines and create a dictionary that maps the forwards-spelling to the backwards spelling. After the data is read in, the function should return the dictionary back to main.

(3) Input file

Next, add the code to ask the user for the input word grid file and open it up. This could just go right in main, or in a separate function that returns the file to main.

(4) Horizontal Mode

Horizontal mode is a bit easier than vertical mode, so I recommend that you work on this first. Create a function for this, and pass it the word dictionary, the grid file, and the mode. Iterate through the lines of the file one-at-a-time. For each line you encounter, you can search for all of the words in the word dictionary to see if you can find it.

If you try to do all of this in one function, you will probably end up with 3 or 4 levels of nested loops, which can get rather disorganized. I recommend that you create another function, perhaps called something like search_within_line that handles processing one line of searching. The horizontal function can call this for each line that it encounters.

There will be separate test cases for horizontal and for vertical. I recommend that you get the horizontal ones to pass before oving on to vertical.

(5) Vertical Mode

In vertical mode, you have to search for the words in the columns of the grid, rather than the rows. This is a bit more complex, but still very do-able. The way you should go about this is create a loop where you iterate through the column indexes. Within this loop, create another loop that goes through every character in that column and creates a list of those characters. Then, you can treat this column as if it is a row, and pass it into the search_within_line function that you wrote in the last step.

Rules, Comments, Style

You should follow the style rules described in the class style guide. Also, you are requred to use a dictionary for the forwards / backwards word component of this PA. If you have any questions about style, comments, formatting, etc please ask!

Due Date

This PA is due on Tuesday, March 30th at 7pm. Turn in the program via Gradescope. You should make sure that all of the test cases pass before you turn it in. You can still submit without all of the cases passing, but that is not preferable.



This PA was developed in conjuntion with Samantha Mathis, one of our excellent course coords!