|
stone Atone aLone Clone clonS cOons coNns conEs coneY Money |
Word ladders were invented by Lewis Carroll in 1878, the author of Alice in Wonderland. A ladder is a sequence of words that starts at the starting word, ends at the ending word, In a word ladder puzzle you have to change one word into another by altering a single letter at each step. Each word in the ladder must be a valid English word and must have the same length. For example, to turn stone into money, one possible ladder is given on the left. Many ladder puzzles have more than one possible solution. Your program must determine the shortest word ladder. Another path from stone to money is stone store shore chore choke choky cooky cooey coney money Instructions Your program will accept starting and ending words from the input file called "infile.txt". Then, you read the dictionary file called “dictionary.txt '' store all words in a LinkedList. Finally, you build a word ladder between starting and ending words There are several ways to solve this problem. One simple method involves using stacks and queues. The algorithm (that you must implement) works as it follows Get the starting word and search through the dictionary to find all words that are one letter different. Create stacks for each of these words, containing the starting word (pushed first) and the word that is one letter different. Enqueue each of these stacks into a queue. This will create a queue of stacks! Then dequeue the first item (which is a stack) from the queue, look at its top word and compare it with the ending word. If they equal, you are done - this stack contains the ladder. Otherwise, you find all the words one letter different from it. For each of these new words create a deep copy of the stack and push each word onto the stack. Then enqueue those stacks to the queue. And so on. You terminate the process when you reach the ending word or the queue is empty. You have to keep track of used words! Otherwise, an infinite loop occurs. Example The starting word is smart. Find all the words one letter different from smart, push them into different stacks and store stacks in the queue. This table represents a queue of stacks. ---------------------------------------------------- Now dequeue the front and find all words one letter different from the top word scart. This will spawn seven stacks:
--------------------------------------------------------------------- which we enqueue to the queue. The queue size now is 11. Again dequeue the front and find all words one letter different from the top word start. This will spawn four stacks: ---------------------------------------- Add them to the queue. The queue size now is 14. Repeat the procedure until either you find the ending word or such a word ladder does not exist. Make sure you do not run into an infinite loop! Queue You are to implement a Queue data structure based on Java’s Queue class using LinkedList. Stack You are to use Java's Stack class. Output Your program must output to the console one word ladder from the start word to the end word. Every word in the ladder must be a word that appears in the dictionary. This includes the given start and end words--if they are not in the dictionary, you should print "There is no word ladder between ..." Remember that there may be more than one ladder between the start word and the end word. Your program may output any one of these ladders. The first output word must be the start word and the last output word must be the end word. If there is no way to make a ladder from the start word to the end word, your program must output "There is no word ladder between ..." |
import java.util.*;
import java.io.*;
public class WordLadder {
private static LinkedList< String >
dict;
private static LinkedList< String >
visited;
private static String start, end;
public static void main(String[] args) throws
IOException{
// TODO Auto-generated method stub
File dictfile = new
File("dictionary.txt");
File infile = new
File("infile.txt");
dict = new
LinkedList<>();
// load the dictionary
try(
Scanner in = new
Scanner(dictfile);){
while(in.hasNext()) {
dict.add(in.next());
}
}
try(Scanner in = new
Scanner(infile);)
{
while(in.hasNext()) {
start = in.next();
end = in.next();
if(start.length()!=end.length() || !dict.contains(start) ||
!dict.contains(end) ){
System.out.println("There is no word ladder between "+start+ " and
"+end);
continue;
}
findLadder(start,end);
}
}
}
public static void findLadder(String start,String end)
{
Queue< Stack< String >>
queue = new LinkedList<>();
visited = new
LinkedList<>();
Stack< String > copiedStack =
new Stack<>();
// Left as exercise
}
public static boolean isAnEdge(String w1, String w2)
{
// Left as
exercise
}
Output:
[line, fine]
[dear, fear]
There is no word ladder between stone and money
[fake, lake, lase, last, cast, cost, coat]
[like, line, fine, fire, fore, core, cord, cold, hold, held, help]
There is no word ladder between blue and pink
[face, fake, lake, like]
[help, held, hold, cold, cord, core, fore, fire, fine, find, mind]
[lice, line, fine, fire, fore, core, cord, cold, hold, held, help]
There is no word ladder between door and lice
In: Computer Science
Prove that if two non-equal letters are interchanged in a ISBN code word the error will be detected (the word is no longer an ISBN code word)
In: Advanced Math
Given the following memory values and address instruction with an accumulator. Determine the values with the following instructions load into accumulator.
Word 16 contains 22
Word 18 contains 24
Word 20 contains 26
Word 22 contains 28
Word 24 contains 30
Word 26 contains 32
Word 28 contains 34
i. LOAD IMMEDIATE 16
ii. LOAD DIRECT 16
iii. LOAD INDIRECT 16
iv. LOAD IMMEDIATE 18
v. LOAD DIRECT 18
vi. LOAD INDIRECT 20
vii. LOAD IMMEDIATE 24
viii. LOAD DIRECT 26
ix. LOAD INDIRECT 22
x. LOAD DIRECT 28
[10 Marks]
In: Computer Science
Write a program to allow a user to play the game Hangman. DO NOT USE AN ARRAY
The program will generate a random number (between 1 and 4581) to pick a word from the file - this is the word you then have to guess.
Note: if the random number you generate is 42, you need the 42nd word - so loop 41 times picking up the word from the file and not doing anything with it, it is the 42nd you need. Here is how you will do this:
String word:
for (int k=0; k<41; k++)
{word=scnr.nextLine();
}//end loop and now pick up the word you want
word=scnr.nextLine() //this is the one you want
The user will be allowed to guess a letter as many times as it takes - but 10 wrong guesses and they lose!!
Eventually either they won or lost. In case they lost print out the answer.
Code language: Java using JGrasp or NetBeans
In: Computer Science
Python3 Please add comments
Write a program that implements the word guessing game
- There is a secret word
- The user enters letters one at a time, if the letter appears in the secret word, it is revealed. Otherwise, it counts as a wrong guess.
If the user reveals all the letters in the word before getting too many wrong guesses then they win!
Otherwise, they lose.
1 - define secret word
2 - create a revealed letter list that starts empty
2. set wrong guess count to 0
3. ask the user for a letter
4. is the letter in the secret word?
5. if so: add it to the revealed letters list
create a blank string
for each letter in the secret word, if the letter in
revealed letter list, add that letter to string,
otherwise add an underscore to string
6. if letter not in secret word, add one to wrong guess
7. repeat from step 3
In: Computer Science
in java (just a line of code will do)
Your program will read in the text file, word by word. You are to ignore leading and trailing punctuation and capitalization (i.e., "Castle", "castle?", and "CASTLE" are all equivalent to castle). Convert all words to lower case on input, or you can use case-insensitive comparisons - your choice. You will be putting the words into several implementations of linked lists and comparing the results. If, after trimming punctuation, a "word" consists of no letters, then discard the word. Such a case might arise if your word-reading strategy assumes that every word is delimited by spaces, in which case, a dash would constitute a "word", but would not contain any letters, so it would be discarded (for example, "comparisons - your choice" should be three words, though your space-delimited parser might identify "-" as a fourth "word", but the “-“ should be discarded).
In: Computer Science
In: Economics
Write a C++ program that inputs (cin) a word from the keyboard and generates several different scrambles of the word without using vectors. The input word can be from 4 to 10 letters in length. The number of scrambles produced depends on the number of letters in the word. e.g. the 4 letter word “lose” would have 4 different scrambles produced and the seven letter word “edition” would have seven different scrambles.
Here is a candidate example:
Input: FLOOR
Scrambles: ROOLF, OOLFR, OLFRO, LOORF, OORFL
Your program must use the same block of code for each input word. There is also the rand() random number generator available.
Test you program with these words:
salt
asteroid
manipulate
In: Computer Science
I wrote this code and it produces a typeError, so please can you fix it?
import random
def first_to_a_word():
print("###### First to a Word ######")
print("Instructions:")
print("You will take turns choosing letters one at a time until a
word is formed.")
print("After each letter is chosen you will have a chance to
confirm whether or not a word has been formed.")
print("When a word is formed, the player who played the last letter
wins!")
print("One of you has been chosen at random to initiate the
game.")
print()
print("Note: Words must be longer than a single letter!")
print()
#inputs / randomNumGen
player1 = input("Enter player 1's name: ")
player2 = input("Enter player 2's name: ")
random_number = random.randint(0, 1)
#output function
def output_func(player1, player2, random_number):
if random_number == 0:
word = input(player1, 'please enter a character: ')
print(player1, 'input:', word)
else:
word = input(player2, 'please enter a character: ')
print(player2, 'input:', word)
return word
#initial variables
word_confirm = ''
counter = 0
word_array = []
#character input loop
while word_confirm != 'yes':
#function call
word = output_func(player1, player2, random_number)
#append
word_array.append(word)
#alternate players turns
if random_number == 0:
random_number = 1
else:
random_number = 0
#check for exit after >=3 letters formed
if counter >= 2:
print('You have formed the letters:') #three letters
word_confirm = input('Has the word been formed? (type "yes" or
"no"): ')
word_confirm = word_confirm.lower()
counter += 1
complete_word = ''
i = 0
while i < len(word_array):
complete_word += word_array[i]
i += 1
if random_number == 1:
print(player1, 'Wins!')
print('The word was:', complete_word)
else:
print(player2, 'Wins!')
print('The word was:', complete_word)
first_to_a_word()
In: Computer Science
In: Computer Science