CS 1004 Aug 19 2014
You must embrace what is special about you. I know that sounds like a cat poster, but it’s true
Vitruvious, The LEGO Movie
1 Capstone Homework Data Structures
1.1 Representing complex information
The capstone homework asks you to record information about the QWERTY keyboard. Specifically you need to be able to quickly find all neighboring keys for a given key.
If you reflect on the existing types that you know (i.e., string, int, list) you quickly recognize that you have to develop your own structure. How do you go about this process?
The first step is to determine what the necessary operations will be. Here, you need to find the neighbors for a given key. How should the neighbors be represented? You have seen two aggregate structures that can work.
string – a string of characters
list – a list of strings, each containing a single character
Either one will work. Let’s use the first one and write a function definition:
def neighbors(key): """ Return string containing neighbors of given key (A-Z) """
This is a good start. Now how to go about making this work. I am going to show you one inefficient way, and hopefullly you can improve upon this solution.
I look at the keyboard, and I see three rows from left to right. Let’s represent this keyboard as three strings.
row1 = ’qwertyuiop’ row2 = ’asdfghjkl’ row3 = ’zxcvbnm’
Now, if you could only find the row number that contains the desired key, you can make some computations based upon index values. note how I’ve indented the strings just a bit to show the relationship between keys on one row and keys on the other row. With this visual in mind, the following code works for some of the keys in the second row:
def neighbors(key): """ Return string containing neighbors of given key (A-Z) """ row1 = ’qwertyuiop’ row2 = ’asdfghjkl’ row3 = ’zxcvbnm’ if key in row2: idx = row2.index(key) return row2[idx-1] + row2[idx+1] + row1[idx] + row1[idx+1] + row3[idx-1] + row3[idx] return ’unknown’
This will wnlly work for keys SDFGHJ because these are the ones with six neighbors. Why? Because trying with the other keys in row 2 (such as A, K or L) will cause an exception to be thrown because the index values into row1 and row3 will be undefined.
After thinking about this problem for awhile you should realize that the layout on the keyboard can be assumed to be fixed, in which case you can find an easier implementation for neighbors and I will leave that task to you.
1.2 Building Blocks
Returning to the capstone exercise, your code must find alternatives to words that have a single letter mis-typed. For example, when given "grozen" as the word, it should be able to provide "frozen" as a suitable alternative. This is the current expectation of your homework assignment.
What about words with two mis-typed characters? For example, on my smart phone, when I type "frizrn" it auto-corrects to "frozen". However, "Grizen" does not show any auto-correct. Perhaps, then, we could consider allowing for two mis-typed characters, but not the first or last in the word.
You can write a function to help you. Consider the following function:
def neighbors(word, idx): """ Return list of words where the character word[idx] is replaced in turn by each of its neighbors. """
When called as neighbors(’frizrn’, 2) it returns the list [’fruzrn’, ’frjzrn’, ’frkzrn’, ’frozrn’]. Now for each of these words in turn, you could try neighbors(’the-word’, 3)) and neighbors(’the-word’, 4)) and check to see if any of those strings were words. There would be thirty-two words to check and eventually you would find ’frozen’ in there.
Note that you do not have to implement two-mistyped character autocorrects. I encourage you to think about the building block methods that will form the basis for your implementation. Try to break down the task at hand into small enough pieces, each of which you can implement and verify easily.
1.3 Version : 2014/08/18
(c) 2014, George Heineman