SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
The first question is to write an isValid method in the given code down below.
The second question is to write a method called solveRb in the code below.
Written details about the code that is already provided.
Dictionary Class Code:
LetterSquare Code where we have to add the method:
Problem 7: A Letter Square solver 25 points; pair-optional This is the only problem of the
assignment that you may complete with a partner. See the rules for working with a partner on
pair-optional problems for details about how this type of collaboration must be structured. The
New York Times includes a daily puzzle called "Letter Boxed." It involves a set of 12 letters
arranged around the sides of a square, such as the following: To solve the puzzle, you need to
come up with a sequence of English words in which each letter in the puzzle appears at least
once. In addition, the words in the sequence must observe the following constraints: - Each word
must be at least 3 letters long. - Adjacent letters must come from different sides of the square.
For example, when solving the puzzle above, if the first letter in a word is T, the second letter in
that word cannot be either A or E, since A and E are on the same side of the square as T. One
word that can be formed from the puzzle above is TIME. I is on a different side of the square
than T,M is on a different side than I, and E is on a different side than M. - The last letter of one
word of the sequence must match the first letter in the next word in the sequence. For example, if
we use TIME as the first word in our solution, the second word must then begin with E, since
TIME ends with E. For a given puzzle, there are many possible solutions, but solutions that use
fewer words are preferred. For the puzzle above, one possible solution is
Task 2: implement isValid Once you have reviewed the provided code, you can begin to
implement the bodies of the two methods that you are required to write. You should not change
the headers that we have provided. You should start by implementing the isvalid helper method
that will be used to check if a given letter would work as the next letter in the current word,
given the words and prefixes in the dictionary and the constraints of the puzzle described at the
beginning of the problem. This method must take three parameters: - letter: a single-character
string representing the letter whose validity is being tested - wordNum: an integer specifying the
index of the position in the words array of the word that is currently being built - charNum: an
integer specifying the index of the position within the current word that letter is being considered
for. It should return true if the specified letter is a valid choice for the letter in position charNum
of the word in position wordNum of the words array, and false otherwise. You may assume that
only appropriate values will be passed in. In particular, you may assume that letter is one of the
letters of the puzzle. The constraints that you need to check will depend on the value of the
charNum parameter (and possibly also of the wordNum parameter). For example, let's assume
that we have the following situation: - We are solving the puzzle shown at the start of the
problem (the one with sides {"tae", "nih", "omk", "lys"}). - We are looking for a solution of at
most 2 words. - The current partial solution is { "time", }. - We are within the call this.
solveRB(0,4,2) - i.e., we are attempting to expand the word in position 0 ("time") by finding a
letter that would work in position 4 of that word.
Given this situation: - this. isValid("l", 0,4 ) should return true because " l " is on a different side
of the puzzle than "e" (the letter that was added to give "time") and "timel" is a word and/or a
prefix of a word in the dictionary (which we know because dictionary. hasstring ("timel") returns
true) - this. isvalid("s", 0,4 ) should also return true because "s" is on different side of the puzzle
than "e" and dictionary. hasString("times") returns true - this. isvalid("a", 0,4 ) should return
false because "a" is on the same side of the puzzle as "e" - this. isvalid(" y",0,4 ) should return
false because "timey" is neither a word nor a prefix of a word in the dictionary (which we know
because dictionary. hasString("timey") returns false). Now imagine that we have added the letter
"s" to the partial solution described above to give a new partial solution { "times", ... } and that
we are now focused on the first letter in second word in the solution (i.e., that we are within the
call this. solveRB(1,0,2) ). Given this situation: - this.isvalid("s", 1, 0) should return true because
we are focused on the first letter in a new word and "s" is the last letter of the previous word
("times") - this.isValid("l", 1,0 ) should return false because we are focused on the first letter in a
new word and " l " is not the last letter of the previous word. Other notes: - You should take
advantage of one or more of the methods in the Dictionary object given by the class constant
dictionary. - You will need a special case for handling the first character of the first word in the
solution. In that case, any letter of the puzzle is valid! - When is considering a case in which the
current word is being expanded by one letter, the method should return false if adding the letter
would produce a word that is already part of the solution. Otherwise, you could end up producing
a solution that repeatedly uses the same word (e.g., {"toast", "toast", ...}). Note that we have
given you a helper method that makes it easy to check for this case! - isValid should only
determine if the specified letter is a valid choice for the next letter.
Task 3: implement solveRB The recursive-backtracking method (solveRB) must take three
integer parameters: - wordNum: the index of the position in the words array of the word that is
currently being built - charNum: the index of the character within the current word that this call
is responsible for adding - maxWords: the maximum number of words that the solution is
allowed to have. It should follow the same basic approach as the recursive-backtracking template
from lecture (the one designed to find a single solution). However, there will be a couple of key
differences: - In addition to a base case that checks if the puzzle has been solved, you will need a
second base case for calls in which wordNum is too big, given the value of maxWords. For
example, the call this. solveRB(3,0,3) should return false, because if maxWords is 3 , we
shouldn't be looking for a fourth word (which is what a first input of 3 indicates). - If the current
call is able to add a letter to the solution, you may need to make two separate recursive calls: -
First, you should make a call that attempts to expand the current word in the solution, making it
one letter longer. - Then, if the first recursive call does not succeed in finding a solution and if
the current word in the solution is a full word of at least three letters, you should make a second
recursive call that moves on to the next word in the solution. For example, let's say that the call
this. solveRB(,3,2) adds the letter e to position 3 of the first word in the solution, giving us the
string "time" as that first word. - First, we would make the call this. solveRB(,4,2) to see if we
can expand "time" into a longer word. - Then, if the first call returns false, we would check if
"time" is a full word of at least 3 letters. Because it is, we would make the call this.
solveRB(1,0,2) to move onto the second word in the solution (the one in position 1 of the words
array). Note that we also use a for the second input of this call, since the call will be focused on
the first letter in that new word.
ask 1: review the provided code Ve have provided: - a class called LetterSquare that will serve as
a blueprint for objects that represent a Letter Square puzzle, and that can be used to solve the
puzzle. We have included some starter code, and you will implement two key methods of the
class. - a separate class called Dictionary that serves as a blueprint for a Dictionary object that
you can use to check if a string is a word or the prefix of a word in a collection of common
English words. You should not modify this class in any way. - a text file called word_list.txt
containing the words in the dictionary. Segin by reading over the code that we have given you in
LetterSquare. java. The provided ode includes: - a constant called dictionary that refers to the
Dictionary object described above. The two key methods in this object are: - dictionary.
hasString(s), which returns true if the string s is either a word or the prefix of a word in the
dictionary of words, and false otherwise. For example, because "game" is a word in the
dictionary, all of the following calls will return true: - dictionary.hasString("g") -
dictionary.hasString("ga") - dictionary.hasString("gam") - dictionary.hasString("game")
However, dictionary. hasString("gome") will return false, because the string `" gome" is neither a
word nor the prefix of a word in the dictionary. - dictionary. hasFullWord(s), which returns true
if the string s is a "full word" (i.e., a word that can stand on its own, and is not only a prefix) in
the dictionary of words, and false otherwise. For example: - dictionary. hasFullWord("game")
will return true, because "game" is a full word in the dictionary. - dictionary. hasFullWord("g"),
dictionary. hasFullWord("ga"), and dictionary. hasFullWord("gam") will all return false, because
these strings are not full words in the dictionary.
- a field called sides that refers to an array of strings. It is used to store the letters on each side of
the puzzle, with each side represented by a three-letter string. For example, the sides of the
puzzle above would be stored as the following array: {"tae","nih","omk","lys"} (Either lower-
case or upper-case characters can be used.) - a field called letters that refers to an array of single-
letter strings. This array is used to store the individual letters in the puzzle. For example, the
letters in the puzzle above would be stored as the following array:
{"t","a","e","n","i","h","o","m","k","l","y","s"} (Note: We are using an array of String objects,
not an array of char values. Doing so simplifies some of the necessary constraint-checking. In
particular, it allows us to use the built-in contains method from the String class to determine if a
given letter is found within a given string.) - a field called words that refers to an array of strings.
It will be used to store the words in the solution to the puzzle. When the LetterSquare object is
created, this array is initially filled with empty strings. - a constructor that takes an array
representing the sides of the puzzle and initializes the fields. - a toString method that returns a
string representation of the puzzle that will be used when you print a LetterSquare object. -
private static helper methods that make it easier to perform two types of operations on String
objects: - one called lastLetter(word) that can be used to obtain a single-letter string consisting of
the last letter in the string word; for example, lastLetter ("world") would return the string "d". -
one called removeLast(word) that takes a string word and returns the new string formed by
removing the last letter in word; for example, removeLast("world") would return the string
"worl".
- a private method called addLetter that takes a single-character string letter and an integer
wordNum, and that adds the specified letter to the end of the word in position wordNum of the
words array. - a private method called removeLetter that takes an integer wordNum and that
removes the last letter from the word in position wordNum in the words array. - a private method
called al readyUsed that takes an arbitrary string word and that returns the boolean value true if
word is already one of the words in the solution, and false otherwise. - a private method called
onSameSide that takes two single-character strings letter1 and letter2, and that returns true if
letter1 and letter2 are on the same side of the puzzle, and false otherwise. - a private method
called allLettersUsed that returns the boolean value true if all of the letters in the puzzle have
been used somewhere in the solution, and false otherwise. - a private method called print
Solution
that takes an integer wordNum and prints the words in positions 0 through wordNum of the
words array. - the skeleton of a private method called solveRB that you will implement. This is
the recursive-backtracking method, and it should return true if a solution to the puzzle has been
found and false if no solution has been found (i.e., if the method is backtracking). This method
must take three integer parameters: - wordNum: the index of the position in the words array of
the word that is currently being built - charNum: the index of the character within the current
word that this call is responsible for adding - maxWords: the maximum number of words that the
solution is allowed to have. - the skeleton of a private method called isValid that you will also
implement. This method should be called by solveRB to check if a given letter would work as
the next letter in the current word, given the words and prefixes in the dictionary and the
constraints of the puzzle described at the beginning of the problem. This method must take three
parameters:
This method must take three parameters: - letter: a single-character string representing the letter
whose validity is being tested - wordNum: an integer specifying the index of the position in the
words array of the word that is currently being built - charNum: an integer specifying the index
of the position within the current word that letter is being considered for. It should return true if
the specified letter is a valid choice for the letter in position charNum of the word in position
wordNum of the words array, and false otherwise. You may assume that only appropriate values
will be passed in. In particular, you may assume that letter is one of the letters of the puzzle. a
public method named solve that clients can call to solve a LetterSquare puzzle. It repeatedly calls
solveRB with increasing values of maxWords until it finds a solution to the puzzle, or until it has
tried and failed to find solutions of up to 10 words. a main method that allows the user to enter
and solve a puzzle.
Dictionary.java > Zz Dictionary
The first question is to write an isValid method in the given code d.pdf

Mais conteúdo relacionado

Semelhante a The first question is to write an isValid method in the given code d.pdf

Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objects
Harkamal Singh
 
CMPS 5P Assignment 3 Spring 19Instructions1. The aim o.docx
CMPS 5P Assignment 3 Spring 19Instructions1. The aim o.docxCMPS 5P Assignment 3 Spring 19Instructions1. The aim o.docx
CMPS 5P Assignment 3 Spring 19Instructions1. The aim o.docx
mary772
 
Logarithms and logarithmic functions
Logarithms and logarithmic functionsLogarithms and logarithmic functions
Logarithms and logarithmic functions
Jessica Garcia
 
Logarithms and logarithmic functions
Logarithms and logarithmic functionsLogarithms and logarithmic functions
Logarithms and logarithmic functions
Jessica Garcia
 
Special Topics on Functions, Sequences, and Series MAT117 .docx
 Special Topics on Functions, Sequences, and Series MAT117 .docx Special Topics on Functions, Sequences, and Series MAT117 .docx
Special Topics on Functions, Sequences, and Series MAT117 .docx
MARRY7
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raj Gupta
 

Semelhante a The first question is to write an isValid method in the given code d.pdf (20)

Ruby cheat sheet
Ruby cheat sheetRuby cheat sheet
Ruby cheat sheet
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
 
NP-Completes
NP-CompletesNP-Completes
NP-Completes
 
Finite Systems Handling Language (YAFOLL message 1)
Finite Systems Handling Language (YAFOLL message 1)Finite Systems Handling Language (YAFOLL message 1)
Finite Systems Handling Language (YAFOLL message 1)
 
Information Retrieval-05(wild card query_positional index_spell correction)
Information Retrieval-05(wild card query_positional index_spell correction)Information Retrieval-05(wild card query_positional index_spell correction)
Information Retrieval-05(wild card query_positional index_spell correction)
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objects
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
Grade 10 Math Module 1 searching for patterns, sequence and series
Grade 10 Math Module 1   searching for patterns, sequence and seriesGrade 10 Math Module 1   searching for patterns, sequence and series
Grade 10 Math Module 1 searching for patterns, sequence and series
 
CMPS 5P Assignment 3 Spring 19Instructions1. The aim o.docx
CMPS 5P Assignment 3 Spring 19Instructions1. The aim o.docxCMPS 5P Assignment 3 Spring 19Instructions1. The aim o.docx
CMPS 5P Assignment 3 Spring 19Instructions1. The aim o.docx
 
Mathtest 01
Mathtest 01Mathtest 01
Mathtest 01
 
451142320-2-Language-of-Mathematics-SC-pptx.pptx
451142320-2-Language-of-Mathematics-SC-pptx.pptx451142320-2-Language-of-Mathematics-SC-pptx.pptx
451142320-2-Language-of-Mathematics-SC-pptx.pptx
 
DLP 10 Q1 W8.docx
DLP 10 Q1 W8.docxDLP 10 Q1 W8.docx
DLP 10 Q1 W8.docx
 
Python - Lecture 7
Python - Lecture 7Python - Lecture 7
Python - Lecture 7
 
regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdf
 
Logarithms and logarithmic functions
Logarithms and logarithmic functionsLogarithms and logarithmic functions
Logarithms and logarithmic functions
 
Logarithms and logarithmic functions
Logarithms and logarithmic functionsLogarithms and logarithmic functions
Logarithms and logarithmic functions
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Special Topics on Functions, Sequences, and Series MAT117 .docx
 Special Topics on Functions, Sequences, and Series MAT117 .docx Special Topics on Functions, Sequences, and Series MAT117 .docx
Special Topics on Functions, Sequences, and Series MAT117 .docx
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Computer Science Exam Help
Computer Science Exam Help Computer Science Exam Help
Computer Science Exam Help
 

Mais de sgambikaproducts

The following are the work records of 5 employees. As their manager,.pdf
The following are the work records of 5 employees. As their manager,.pdfThe following are the work records of 5 employees. As their manager,.pdf
The following are the work records of 5 employees. As their manager,.pdf
sgambikaproducts
 
The following excerpts have been taking from a patient � Maisie Wils.pdf
The following excerpts have been taking from a patient � Maisie Wils.pdfThe following excerpts have been taking from a patient � Maisie Wils.pdf
The following excerpts have been taking from a patient � Maisie Wils.pdf
sgambikaproducts
 

Mais de sgambikaproducts (20)

The following transaction have not been recorded1. A debtor retur.pdf
The following transaction have not been recorded1. A debtor retur.pdfThe following transaction have not been recorded1. A debtor retur.pdf
The following transaction have not been recorded1. A debtor retur.pdf
 
The following transaction have not been recorded 1. A debtor retu.pdf
The following transaction have not been recorded 1. A debtor retu.pdfThe following transaction have not been recorded 1. A debtor retu.pdf
The following transaction have not been recorded 1. A debtor retu.pdf
 
The following table contains nominal and real GDP data in billions o.pdf
The following table contains nominal and real GDP data in billions o.pdfThe following table contains nominal and real GDP data in billions o.pdf
The following table contains nominal and real GDP data in billions o.pdf
 
The following picture represents a Real Madrid football fans custom.pdf
The following picture represents a Real Madrid football fans custom.pdfThe following picture represents a Real Madrid football fans custom.pdf
The following picture represents a Real Madrid football fans custom.pdf
 
The following list of Rs and Ns represent returned documents in a ra.pdf
The following list of Rs and Ns represent returned documents in a ra.pdfThe following list of Rs and Ns represent returned documents in a ra.pdf
The following list of Rs and Ns represent returned documents in a ra.pdf
 
The following list contains a number of debt instruments which you c.pdf
The following list contains a number of debt instruments which you c.pdfThe following list contains a number of debt instruments which you c.pdf
The following list contains a number of debt instruments which you c.pdf
 
The following are the work records of 5 employees. As their manager,.pdf
The following are the work records of 5 employees. As their manager,.pdfThe following are the work records of 5 employees. As their manager,.pdf
The following are the work records of 5 employees. As their manager,.pdf
 
The following information is given (in millions of euros) about inte.pdf
The following information is given (in millions of euros) about inte.pdfThe following information is given (in millions of euros) about inte.pdf
The following information is given (in millions of euros) about inte.pdf
 
The following information applies to the questions displayed below] .pdf
The following information applies to the questions displayed below] .pdfThe following information applies to the questions displayed below] .pdf
The following information applies to the questions displayed below] .pdf
 
The following income statement was drawn from the records of Munoz, .pdf
The following income statement was drawn from the records of Munoz, .pdfThe following income statement was drawn from the records of Munoz, .pdf
The following income statement was drawn from the records of Munoz, .pdf
 
The following excerpts have been taking from a patient � Maisie Wils.pdf
The following excerpts have been taking from a patient � Maisie Wils.pdfThe following excerpts have been taking from a patient � Maisie Wils.pdf
The following excerpts have been taking from a patient � Maisie Wils.pdf
 
The following events occurred in the recent past in Canada� The w.pdf
The following events occurred in the recent past in Canada� The w.pdfThe following events occurred in the recent past in Canada� The w.pdf
The following events occurred in the recent past in Canada� The w.pdf
 
The following diagram shows the chromosomes with their associated ge.pdf
The following diagram shows the chromosomes with their associated ge.pdfThe following diagram shows the chromosomes with their associated ge.pdf
The following diagram shows the chromosomes with their associated ge.pdf
 
The firm is currently an all-equity firm with assets worth $250 mill.pdf
The firm is currently an all-equity firm with assets worth $250 mill.pdfThe firm is currently an all-equity firm with assets worth $250 mill.pdf
The firm is currently an all-equity firm with assets worth $250 mill.pdf
 
The financial records of Vaughn Inc. were destroyed by fire at the e.pdf
The financial records of Vaughn Inc. were destroyed by fire at the e.pdfThe financial records of Vaughn Inc. were destroyed by fire at the e.pdf
The financial records of Vaughn Inc. were destroyed by fire at the e.pdf
 
The figure above shows graphs of oxygen consumption and of ATP synth.pdf
The figure above shows graphs of oxygen consumption and of ATP synth.pdfThe figure above shows graphs of oxygen consumption and of ATP synth.pdf
The figure above shows graphs of oxygen consumption and of ATP synth.pdf
 
The dirname utility treats its argument as a pathname and wri.pdf
The dirname utility treats its argument as a pathname and wri.pdfThe dirname utility treats its argument as a pathname and wri.pdf
The dirname utility treats its argument as a pathname and wri.pdf
 
The Federal Register is best described by which of the following sta.pdf
The Federal Register is best described by which of the following sta.pdfThe Federal Register is best described by which of the following sta.pdf
The Federal Register is best described by which of the following sta.pdf
 
The Federal Information Security Modernization Act (FISMA) was desig.pdf
The Federal Information Security Modernization Act (FISMA) was desig.pdfThe Federal Information Security Modernization Act (FISMA) was desig.pdf
The Federal Information Security Modernization Act (FISMA) was desig.pdf
 
The FASB requires that a statement showing the relationship of funct.pdf
The FASB requires that a statement showing the relationship of funct.pdfThe FASB requires that a statement showing the relationship of funct.pdf
The FASB requires that a statement showing the relationship of funct.pdf
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

The first question is to write an isValid method in the given code d.pdf

  • 1. The first question is to write an isValid method in the given code down below. The second question is to write a method called solveRb in the code below. Written details about the code that is already provided. Dictionary Class Code: LetterSquare Code where we have to add the method: Problem 7: A Letter Square solver 25 points; pair-optional This is the only problem of the assignment that you may complete with a partner. See the rules for working with a partner on pair-optional problems for details about how this type of collaboration must be structured. The New York Times includes a daily puzzle called "Letter Boxed." It involves a set of 12 letters arranged around the sides of a square, such as the following: To solve the puzzle, you need to come up with a sequence of English words in which each letter in the puzzle appears at least once. In addition, the words in the sequence must observe the following constraints: - Each word must be at least 3 letters long. - Adjacent letters must come from different sides of the square. For example, when solving the puzzle above, if the first letter in a word is T, the second letter in that word cannot be either A or E, since A and E are on the same side of the square as T. One word that can be formed from the puzzle above is TIME. I is on a different side of the square than T,M is on a different side than I, and E is on a different side than M. - The last letter of one word of the sequence must match the first letter in the next word in the sequence. For example, if we use TIME as the first word in our solution, the second word must then begin with E, since TIME ends with E. For a given puzzle, there are many possible solutions, but solutions that use fewer words are preferred. For the puzzle above, one possible solution is Task 2: implement isValid Once you have reviewed the provided code, you can begin to implement the bodies of the two methods that you are required to write. You should not change the headers that we have provided. You should start by implementing the isvalid helper method that will be used to check if a given letter would work as the next letter in the current word, given the words and prefixes in the dictionary and the constraints of the puzzle described at the beginning of the problem. This method must take three parameters: - letter: a single-character string representing the letter whose validity is being tested - wordNum: an integer specifying the index of the position in the words array of the word that is currently being built - charNum: an integer specifying the index of the position within the current word that letter is being considered for. It should return true if the specified letter is a valid choice for the letter in position charNum
  • 2. of the word in position wordNum of the words array, and false otherwise. You may assume that only appropriate values will be passed in. In particular, you may assume that letter is one of the letters of the puzzle. The constraints that you need to check will depend on the value of the charNum parameter (and possibly also of the wordNum parameter). For example, let's assume that we have the following situation: - We are solving the puzzle shown at the start of the problem (the one with sides {"tae", "nih", "omk", "lys"}). - We are looking for a solution of at most 2 words. - The current partial solution is { "time", }. - We are within the call this. solveRB(0,4,2) - i.e., we are attempting to expand the word in position 0 ("time") by finding a letter that would work in position 4 of that word. Given this situation: - this. isValid("l", 0,4 ) should return true because " l " is on a different side of the puzzle than "e" (the letter that was added to give "time") and "timel" is a word and/or a prefix of a word in the dictionary (which we know because dictionary. hasstring ("timel") returns true) - this. isvalid("s", 0,4 ) should also return true because "s" is on different side of the puzzle than "e" and dictionary. hasString("times") returns true - this. isvalid("a", 0,4 ) should return false because "a" is on the same side of the puzzle as "e" - this. isvalid(" y",0,4 ) should return false because "timey" is neither a word nor a prefix of a word in the dictionary (which we know because dictionary. hasString("timey") returns false). Now imagine that we have added the letter "s" to the partial solution described above to give a new partial solution { "times", ... } and that we are now focused on the first letter in second word in the solution (i.e., that we are within the call this. solveRB(1,0,2) ). Given this situation: - this.isvalid("s", 1, 0) should return true because we are focused on the first letter in a new word and "s" is the last letter of the previous word ("times") - this.isValid("l", 1,0 ) should return false because we are focused on the first letter in a new word and " l " is not the last letter of the previous word. Other notes: - You should take advantage of one or more of the methods in the Dictionary object given by the class constant dictionary. - You will need a special case for handling the first character of the first word in the solution. In that case, any letter of the puzzle is valid! - When is considering a case in which the current word is being expanded by one letter, the method should return false if adding the letter would produce a word that is already part of the solution. Otherwise, you could end up producing a solution that repeatedly uses the same word (e.g., {"toast", "toast", ...}). Note that we have given you a helper method that makes it easy to check for this case! - isValid should only determine if the specified letter is a valid choice for the next letter. Task 3: implement solveRB The recursive-backtracking method (solveRB) must take three integer parameters: - wordNum: the index of the position in the words array of the word that is currently being built - charNum: the index of the character within the current word that this call
  • 3. is responsible for adding - maxWords: the maximum number of words that the solution is allowed to have. It should follow the same basic approach as the recursive-backtracking template from lecture (the one designed to find a single solution). However, there will be a couple of key differences: - In addition to a base case that checks if the puzzle has been solved, you will need a second base case for calls in which wordNum is too big, given the value of maxWords. For example, the call this. solveRB(3,0,3) should return false, because if maxWords is 3 , we shouldn't be looking for a fourth word (which is what a first input of 3 indicates). - If the current call is able to add a letter to the solution, you may need to make two separate recursive calls: - First, you should make a call that attempts to expand the current word in the solution, making it one letter longer. - Then, if the first recursive call does not succeed in finding a solution and if the current word in the solution is a full word of at least three letters, you should make a second recursive call that moves on to the next word in the solution. For example, let's say that the call this. solveRB(,3,2) adds the letter e to position 3 of the first word in the solution, giving us the string "time" as that first word. - First, we would make the call this. solveRB(,4,2) to see if we can expand "time" into a longer word. - Then, if the first call returns false, we would check if "time" is a full word of at least 3 letters. Because it is, we would make the call this. solveRB(1,0,2) to move onto the second word in the solution (the one in position 1 of the words array). Note that we also use a for the second input of this call, since the call will be focused on the first letter in that new word. ask 1: review the provided code Ve have provided: - a class called LetterSquare that will serve as a blueprint for objects that represent a Letter Square puzzle, and that can be used to solve the puzzle. We have included some starter code, and you will implement two key methods of the class. - a separate class called Dictionary that serves as a blueprint for a Dictionary object that you can use to check if a string is a word or the prefix of a word in a collection of common English words. You should not modify this class in any way. - a text file called word_list.txt containing the words in the dictionary. Segin by reading over the code that we have given you in LetterSquare. java. The provided ode includes: - a constant called dictionary that refers to the Dictionary object described above. The two key methods in this object are: - dictionary. hasString(s), which returns true if the string s is either a word or the prefix of a word in the dictionary of words, and false otherwise. For example, because "game" is a word in the dictionary, all of the following calls will return true: - dictionary.hasString("g") - dictionary.hasString("ga") - dictionary.hasString("gam") - dictionary.hasString("game") However, dictionary. hasString("gome") will return false, because the string `" gome" is neither a word nor the prefix of a word in the dictionary. - dictionary. hasFullWord(s), which returns true if the string s is a "full word" (i.e., a word that can stand on its own, and is not only a prefix) in
  • 4. the dictionary of words, and false otherwise. For example: - dictionary. hasFullWord("game") will return true, because "game" is a full word in the dictionary. - dictionary. hasFullWord("g"), dictionary. hasFullWord("ga"), and dictionary. hasFullWord("gam") will all return false, because these strings are not full words in the dictionary. - a field called sides that refers to an array of strings. It is used to store the letters on each side of the puzzle, with each side represented by a three-letter string. For example, the sides of the puzzle above would be stored as the following array: {"tae","nih","omk","lys"} (Either lower- case or upper-case characters can be used.) - a field called letters that refers to an array of single- letter strings. This array is used to store the individual letters in the puzzle. For example, the letters in the puzzle above would be stored as the following array: {"t","a","e","n","i","h","o","m","k","l","y","s"} (Note: We are using an array of String objects, not an array of char values. Doing so simplifies some of the necessary constraint-checking. In particular, it allows us to use the built-in contains method from the String class to determine if a given letter is found within a given string.) - a field called words that refers to an array of strings. It will be used to store the words in the solution to the puzzle. When the LetterSquare object is created, this array is initially filled with empty strings. - a constructor that takes an array representing the sides of the puzzle and initializes the fields. - a toString method that returns a string representation of the puzzle that will be used when you print a LetterSquare object. - private static helper methods that make it easier to perform two types of operations on String objects: - one called lastLetter(word) that can be used to obtain a single-letter string consisting of the last letter in the string word; for example, lastLetter ("world") would return the string "d". - one called removeLast(word) that takes a string word and returns the new string formed by removing the last letter in word; for example, removeLast("world") would return the string "worl". - a private method called addLetter that takes a single-character string letter and an integer wordNum, and that adds the specified letter to the end of the word in position wordNum of the words array. - a private method called removeLetter that takes an integer wordNum and that removes the last letter from the word in position wordNum in the words array. - a private method called al readyUsed that takes an arbitrary string word and that returns the boolean value true if word is already one of the words in the solution, and false otherwise. - a private method called onSameSide that takes two single-character strings letter1 and letter2, and that returns true if letter1 and letter2 are on the same side of the puzzle, and false otherwise. - a private method called allLettersUsed that returns the boolean value true if all of the letters in the puzzle have been used somewhere in the solution, and false otherwise. - a private method called print
  • 5. Solution that takes an integer wordNum and prints the words in positions 0 through wordNum of the words array. - the skeleton of a private method called solveRB that you will implement. This is the recursive-backtracking method, and it should return true if a solution to the puzzle has been found and false if no solution has been found (i.e., if the method is backtracking). This method must take three integer parameters: - wordNum: the index of the position in the words array of the word that is currently being built - charNum: the index of the character within the current word that this call is responsible for adding - maxWords: the maximum number of words that the solution is allowed to have. - the skeleton of a private method called isValid that you will also implement. This method should be called by solveRB to check if a given letter would work as the next letter in the current word, given the words and prefixes in the dictionary and the constraints of the puzzle described at the beginning of the problem. This method must take three parameters: This method must take three parameters: - letter: a single-character string representing the letter whose validity is being tested - wordNum: an integer specifying the index of the position in the words array of the word that is currently being built - charNum: an integer specifying the index of the position within the current word that letter is being considered for. It should return true if the specified letter is a valid choice for the letter in position charNum of the word in position wordNum of the words array, and false otherwise. You may assume that only appropriate values will be passed in. In particular, you may assume that letter is one of the letters of the puzzle. a public method named solve that clients can call to solve a LetterSquare puzzle. It repeatedly calls solveRB with increasing values of maxWords until it finds a solution to the puzzle, or until it has tried and failed to find solutions of up to 10 words. a main method that allows the user to enter and solve a puzzle. Dictionary.java > Zz Dictionary