Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Level 2 ( weight: 7 5 % ) Once the basic function haveSameLetters has been completed, you can use it to accomplish the task for

Level 2(weight: 75%)
Once the basic function haveSameLetters has been completed, you can use it to accomplish the
task for Level 2. To run Level 2, you simply have to pass in something other than 1 you could pass in
2, or you could simply pass in no command-line argument at all.
When the command line argument is not 1, the code in main() branches off to the else block.
This is where you need to place your code for Level 2. We have already provided the first part: words
are read in from standard input, and they are stored one by one inside a vector (we are using the
Standard Template Library vector class for C++). In the next part of the code, you need to access
the words inside the vector, and print out all groups of words that have exactly the same letters.
Groups of words should be separated by commas (no spaces), and each group should appear on a
new line.
For example, if our input is
sore cake rose leaf teal fear late
Then the output should be
sore,rose
teal,late
(words should appear in the order in which they were entered into the vector.)
If the input is
earth heart hater atom moat mother
then the output should be
earth,heart,hater
atom,moat
Note that there are several tests in Level 2, and it carries more weight than Level 1. It may be easier
to solve the problem for just pairs of words, compared to groups of 3 or more words. If you are
finding it difficult to solve this more challenging problem, you can still get partial credit for solving it
for examples where there are groups of two onlyfor above code
i got Internet: disabled
Timeout:
2
m
0
s
Match type: full
Match case: sensitive
Match whitespaces: include
Program:
# Run the bash script and check the match blocks.
.
/
letters
Input:
star art rats
Expected output:
star,rats
Input:
pig lag alp gal elk eke pal
Expected output:
lag,gal
alp,pal
Input:
pig lap lag alp gal elk eke pal
Expected output:
lap,alp,pal
lag,gal
Input:
meat
team
able
tame
bale
mate
lab
oh
ho
Expected output:
meat,team,tame,mate
able,bale
oh
,
ho
solve please
#include
#include
#include
#include
#include
bool haveSameLetters(const std::string& str1, const std::string& str2){
std::string sorted_str1= str1;
std::string sorted_str2= str2;
std::sort(sorted_str1.begin(), sorted_str1.end());
std::sort(sorted_str2.begin(), sorted_str2.end());
return sorted_str1== sorted_str2;
}
int main(int argc, char** argv){
char *end;
long which;
which =(argc >1)? strtol(argv[1], &end, 10) : 0;
if (which ==1){// Level 1
std::string word1, word2;
std::cin >> word1>> word2;
std::cout << std::boolalpha << haveSameLetters(word1, word2);
} else {// Level 2
std::vector v;
std::string word;
while (std::cin >> word){
v.push_back(word);
}
std::unordered_map> anagramGroups;
for (const std::string& word : v){
std::string sorted_word = word;
std::sort(sorted_word.begin(), sorted_word.end());
anagramGroups[sorted_word].push_back(word);
}
bool firstGroup = true;
for (const auto& pair : anagramGroups){
if (!firstGroup){
std::cout << std::endl;
} else {
firstGroup = false;
}
bool firstWord = true;
for (const std::string& anagram : pair.second){
if (!firstWord){
std::cout <<",";
} else {
firstWord = false;
}
std::cout << anagram;
}
}
return 0;
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions