The program handin21 will only submit files in the cs21/homework/11 directory.
Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page
Some of the problems will have optional components that allow you to
further practice your skills in Python. Optional portions will not be
graded, but may be interesting for those wanting some extra challenges.
For example, to change the start word COOL into the goal
word WARM, a player could take the following steps:
C O O L W O O L W O O D W O R D W A R D W A R M
The start word is: COOL The goal word is: WARM Enter the next word: COOK Enter the next word: CORK Enter the next word: CARK Sorry, CARK is not a word. Try again. Enter the next word: WORK Enter the next word: WARM Sorry, you changed too many letters. Try again. Enter the next word: WORM Enter the next word: WORK You won in 5 steps: COOL, COOK, CORK, WORK, WORM, WARM
The start word is: JACK The goal word is: KING Enter the next word: HACK Enter the next word: HOCK Enter the next word: POCK Enter the next word: PICK Enter the next word: SICK Enter the next word: SACK Enter the next word: SACS Enter the next word: SAWS Enter the next word: SEWS Enter the next word: MEWS Enter the next word: ? Here is a possible solution: JACK, HACK, HICK, KICK, KINK, KINGTo solve a word ladder, we'll use a data structure called a queue. For those of you up on your British English, a queue is another word for a line, such as the one you wait in to buy a ticket for a movie. We will implement a queue using a linked list: People get into the line at the end (insertAtTail) and they get out of the line at the front (removeFromHead).
Here's how the word ladder solver is going to work. To begin with, we're going to create a queue (an empty LinkedList) where each 'data' element we're going to put in the queue will be a list representing the word ladder you've made so far.
At the start of the game, we haven't made much progress on our word ladder, so we'll insert (always at the end for a queue) a list consisting of only the start word. Assuming we're trying to solve WARM to COOL, we'll start by inserting the list ['warm'] into our queue. So, at this point, our queue will contain only one item: the list ['warm'].
Now, we'll enter a loop which we won't exit until either we reach the goal or we reach a dead end (there are no steps that we can take which without backtracking to a word we've already used). Inside that loop, we'll do the following:
And after another iteration we'll have:
['warm', 'worm'],
['warm', 'ward'], ['warm', 'ware'], ['warm', 'warn'], ['warm',
'warp'], ['warm', 'wars'], ['warm', 'wart'], ['warm', 'wary'],
['warm', 'farm', 'firm'], ['warm', 'farm', 'form'], ['warm', 'farm',
'fare'], ['warm', 'farm', 'fart'], ['warm', 'harm', 'hard'], ['warm',
'harm', 'hare'], ['warm', 'harm', 'hark'], ['warm', 'harm', 'harp'],
['warm', 'harm', 'hart']
Notice that all the ladders of length 2 are before the ladders of length 3 in the queue. This ensures us that when we finally find our goal word, we know that it was the shortest possible ladder.
If you find a ladder from the start to the goal, return the ladder; otherwise, return the empty list. (For example, you cannot form a word ladder from ANGEL to DEVIL.)
The word ladder for HOT to COLD:
H O T C O T C O D C O L D
The word ladder for LINKED to LIST:
Your program should allow the user to specify if they would like to
play with insertions and deletions or if they would like to play the
standard way. If they want to play with insertions and deletions, you
should allow for choosing random words of different lengths from the
dictionary.L I N K E D L I N E D L I N E L I N T L I S T
A note of warning: allowing for insertions and deletions will make your program slower, so try some easy cases first.