WordLadder JAVA Program question
|
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 Objectives
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. Dictionary You read the dictionary file. (POSTED AT THE BOTTOM) The dictionary file contains exactly one word per line. The number of lines in the dictionary is not specified. |
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 ..."
For testing purposes, I provide you with the input file and the expected output.
SAMPLE 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
Starter Code:
WordLadder.java
WordLadder.java CONTENT
import java.util.*;
import java.io.*;
public class WordLadder {
private static LinkedList dict;
private static LinkedList 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> queue = new LinkedList<>();
visited = new LinkedList<>();
Stack copiedStack = new Stack<>();
// Left as exercise
}
public static boolean isAnEdge(String w1, String w2) {
// Left as exercise
}
}
infile.txt CONTENT
line fine
dear fear
stone money
fake coat
like help
blue pink
face like
help mind
lice help
door lice
dictionary.txt CONTENT
lice
deck
cord
bent
band
cast
bike
cash
card
boat
cold
coat
dear
slow
core
dash
cost
dame
fish
dorm
dine
deer
dear
dime
fast
blue
deme
dive
dish
dinn
door
dome
fake
slow
pink
face
find
fast
fire
fear
fine
finn
help
held
hash
fore
folk
fold
hard
hear
here
host
hold
hire
lase
land
knot
lake
kunn
kuns
last
mind
main
line
lime
like
lost
live
linn
love
lunn
mike
maze
mash
make
mice
meta
mien
milk
vice
silk
neck
mink
mine
must
most
more
nash
sick
nice
rain
pour
pine
nick
pain
nine
nuns
pond
pony
poor
sake
rick
rash
rime
rust
sane
sand
sine
sure
sony
tiny
warm
vide
ward
worm
In: Computer Science
In: Advanced Math
C++
Project 6-2: Pig Latin Translator
Create a program that translates English to Pig Latin.
Console
Pig Latin Translator
This program translates a sentence
and removes all punctuation from it.
Enter a sentence: 'Tis but a scratch.
Translation: Istay utbay away atchscray
Specifications
Note
In: Computer Science
CALM = ['good', 'safe', 'well', 'benefit', 'ok'] And some possible worried words: WORRIED = ['bad', 'risk', 'risky', 'nervous', 'problem'] Make sure all of your words are lowercase, and that each string is a word and not a phrase -- keeps things simpler. Now weight the words from the above lists. We’ve started out with lists of calm/worried words, but some of them are stronger than others. For example, the word good is probably more positive than the word ok. Assign a weight to each word in your lists. Use positive weights for calm words and negative weights for worried words. Your lists should be 2-dimensional now, in this format: [[word, weight], [word, weight]...] Use list concatenation (+) to create one big list instead of two separate ones, something like this: ALL_WORDS = CALM + WORRIED Iterate over this new 2-dimensional list and print out each word and its corresponding weight.
(Script for ATOM or REPL to be run)
In: Computer Science
IN PYTHON
INF 120 – Project #6.5
In project 6.5 we are going to create a program and test cases to test each path/branch through the program.
Our program is going to get a word from the user and check if it is a palindrome. If it is it will output to the user that it is a palindrome. A palindrome is where the word is the same forwards as backwards.
For example racecar.If the word isn’t a palindrome then we will “translate” the word to pig Latin according to the following rules.1. If the word begins with a consonant move the 1st letter to the end of the word and then append “ay”. So dog becomes ogday.2. If the words begins with a vowel just add “ay” to the end of the word. So elk becomes Elkay.
Write your program and get it working.
Answer the following questions about your program.
1. How many paths/branches are in your program.
2. For each path/branch in your program craft a test case.
3. Run your program with each test case and screenshot the result and include them as the answer to this question.
In: Computer Science
Using your language, don't use any sites (by your understanding), write about the difference between the letter of credit and letter of guarantee
Word Limit for the assignment:
Upper word limit: 1200 words
Lower word limit: 600 words
In: Finance
Write a program that reads a text file and reports the total count of words of each length. A word is defined as any contiguous set of alphanumeric characters, including symbols. For example, in the current sentence there are 10 words. The filename should be given at the command line as an argument. The file should be read one word at a time. A count should be kept for how many words have a given length. For example, the word “frog” is 4 bytes in length; the word “turtle” is 6 bytes in length. The program should report the total word counts of all lengths between 3 and 15 bytes. Words with lengths outside that range should not be counted
In: Computer Science
Python Programming: Scrambled Word Game
Topics: List, tuple
Write a scrambled word game. The game starts by loading a file containing scrambled word-answer pair separated. Sample of the file content is shown below. Once the pairs are loaded, it randomly picks a scrambled word and has the player guess it. The number of guesses is unlimited. When the user guesses the correct answer, it asks the user if he/she wants another scrambled word.
bta:bat
gstoh:ghost
entsrom:monster
Download scrambled.py (code featured below) and halloween.txt. The file scrambled.py already has the print_banner and the load_words functions. The function print_banner simply prints “scrambled” banner. The function load_words load the list of scrambled words-answer pairs from the file and return a tuple of two lists. The first list is the scrambled words and the second is the list of the answers. Your job is the complete the main function so that the game works as shown in the sample run.
Optional Challenge: The allowed number of guess is equal to the length of the word. Print hints such as based on the number of guess. If it’s the first guess, provides no hint. If it’s the second guess, provides the first letter of the answer. If it’s the third guess, provides the first and second letter of the correct answer. Also, make sure the same word is not select twice. Once all words have been used, the game should tell user that all words have been used and terminate.
Sample run:
__
_
_
_
/ _\ ___ _ __ __ _ _ __ ___ | |__ | | ___ __| |
\ \ / __|| '__|/ _` || '_ ` _ \ | '_ \ | | / _ \ / _` |
_\ \| (__ | | | (_| || | | | | || |_) || || __/| (_| |
\__/ \___||_| \__,_||_| |_| |_||_.__/ |_| \___| \__,_|
Scrambled word is: wbe
What is the word? bee
Wrong answer. Try again!
Scrambled word is: wbe
What is the word? web
You got it!
Another game? (Y/N):Y
Scrambled word is: meizob
What is the word? Zombie
You got it!
Another game? (Y/N):N
Bye!
Provided code (from scramble.py
def display_banner():
print("""
__
_
_
_
/ _\ ___ _ __ __ _ _ __ ___ | |__ | | ___ __| |
\ \ / __|| '__|/ _` || '_ ` _ \ | '_ \ | | / _ \ / _` |
_\ \| (__ | | | (_| || | | | | || |_) || || __/| (_| |
\__/ \___||_| \__,_||_| |_| |_||_.__/ |_| \___|
\__,_|
""")
def load_words(filename):
#load file containing scrambled word-answer
pairs.
#scrambled word and answer are sparated by :
scrambled_list = []
answer_list = []
with open(filename, 'r') as f:
for line in f:
(s,a) = line.strip().split(":")
scrambled_list += [s]
answer_list += [a]
return (scrambled_list, answer_list)
def main():
display_banner()
(scrambled_list, answer_list) =
load_words('halloween.txt')
#your code to make the game
work.
main()
Halloween.txt file.
bta:bat
gstoh:ghost
entsrom:monster
ihtcw:witch
meizob:zombie
enetskol:skeleton
rpamevi:vampire
wbe:web
isdepr:spider
umymm:mummy
rboom:broom
nhlwaeeol:Halloween
pkiumnp:pumpkin
kaoa jlern tcn:jack o lantern
tha:hat
claabck t:black cat
omno:moon
aurdclno:caluldron
In: Computer Science
6. Assume a computer has a physical memory organized into 64-bit words. Using hexadecimal notation, give the word address and offset within the word for each of the following byte addresses.
|
Byte address |
Word address |
Offset |
|
0x000b |
||
|
0x03ff |
||
|
0x07fc |
In: Computer Science
Write a Python program that will ask the user to input a word, will return the first letter of the input word, and ask the user to put another word, and so on, in the form of a loop. If the user chooses to stop, he or she should input the integer "0" for the loop to stop.
In: Computer Science