EE 356 Fall 2016 C# WPF – Word Ladders

advertisement
EE 356
C# WPF – Word Ladders
Fall 2016
Word ladders were created by Lewis Carroll in 1877. A word ladder begins with a starting word and an
ending word. For example a starting word may be "ear" and the ending word may be "eye". You must
create intermediate words which differ from the previous word by no more than one letter and connect
the starting word to the ending word.
ear → err → ere → eye
One way to write a program to do this is to create all possibilities in an organized fashion beginning with
the start word and continuing until the ending word is reached.
Set 1 - EAR
Changing only the first letter of ear gives us 8 words:
EAR → bar, car, far, gar, mar, par, tar, war.
Changing only the second letter of ear gives just 1 word:
EAR → err
Changing only the third letter of ear gives just one word:
EAR → eat
Save the Set 1 words in memory and use each word in the set 1 words to find Set 2 words by changing a
single letter.
Set 2 - BAR
Changing only the first letter of bar gives us no new words:
BAR → xxx
Changing only the second letter of bar gives us no new words:
BAR → xxx
Changing only the third letter of bar gives us 6 words:
BAR → bad, bag, bam, ban, bat, bay
Set 3 – CAR
…
Repeat this procedure for all of the set 1 words saving all of the words generated in memory. Note that
you don't keep any words that have already been saved.
The procedure stops when you find the ending word. At the end you can go back through your lists and
find the connecting path.
put the starting word in a list
put the list in a queue
L = length
while not done
{get the next list from the queue
for each word in the list
{create a new list – listNew
for(i=0;i<L;i++)
{change letter i
if the changed word is not in any list
{if the changed word is a real word
{if this is the end word
quit and find ladder
else
add this word to listNew
}
}
}
}
add listNew to the queue
}
From page 382 in the text
Table 10-5. Members of the Queue<T> Type
Select Member of Queue<T>
Meaning in Life
Dequeue()
Removes and returns the object at the beginning of the
Queue<T>.
Enqueue()
Adds an object to the end of the Queue<T>.
Peek()
Returns the object at the beginning of the Queue<T>
without removing it.
static void GetCoffee(Person p)
{Console.WriteLine("{0} got coffee!", p.FirstName);
}
static void UseGenericQueue()
{// Make a Q with three people.
Queue<Person> peopleQ = new Queue<Person>();
peopleQ.Enqueue(new Person{FirstName="Homer",
LastName="Simpson", Age=47});
peopleQ.Enqueue(new Person {FirstName= "Marge",
LastName="Simpson", Age=45});
peopleQ.Enqueue(new Person {FirstName= "Lisa",
LastName="Simpson", Age=9});
// Peek at first person in Q.
Console.WriteLine("{0} is first in line!",
peopleQ.Peek().FirstName);
// Remove each person from Q.
GetCoffee(peopleQ.Dequeue());
GetCoffee(peopleQ.Dequeue());
GetCoffee(peopleQ.Dequeue());
// Try to de-Q again?
try
{GetCoffee(peopleQ.Dequeue());
}
catch(InvalidOperationException e)
{Console.WriteLine("Error! {0}", e.Message);
}
}
***** Fun with Generic Collections *****
Homer is first in line!
Homer got coffee!
Marge got coffee!
Lisa got coffee!
Error! Queue empty.
namespace QueueExample
{/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{public MainWindow()
{InitializeComponent();
}
private static Queue<List<string>> myQueue =
new Queue<List<string>>();
private static Random r = new Random();
private void btnAdd_Click(object sender, RoutedEventArgs e)
{List<string> myList = new List<string>();
int i;
for(i=0;i<3;i++)
myList.Add((r.Next()).ToString());
myQueue.Enqueue(myList);
List<string> myList2 = myQueue.Peek();
for(i=0;i<3;i++)
lstQueue.Items.Add(myList2[i]);
}
private void btnRemove_Click(object sender, RoutedEventArgs e)
{List<string> myList = new List<string>();
try
{myList = myQueue.Dequeue();
lstQueue.Items.Add("Items removed");
int i;
for(i=0;i<3;i++)
lstQueue.Items.Add(myList[i]);
}
catch(InvalidOperationException ee)
{lstQueue.Items.Add("Error: " + ee.Message);
}
}
}
}
Download