Question: Level 2 ( weight: 7 5 % ) Once the basic function haveSameLetters has been completed, you can use it to accomplish the task for
Level weight:
Once the basic function haveSameLetters has been completed, you can use it to accomplish the
task for Level To run Level you simply have to pass in something other than you could pass in
or you could simply pass in no commandline argument at all.
When the command line argument is not the code in main branches off to the else block.
This is where you need to place your code for Level 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 and it carries more weight than Level It may be easier
to solve the problem for just pairs of words, compared to groups of 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:
m
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 haveSameLettersconst std::string& str const std::string& str
std::string sortedstr str;
std::string sortedstr str;
std::sortsortedstrbegin sortedstrend;
std::sortsortedstrbegin sortedstrend;
return sortedstr sortedstr;
int mainint argc, char argv
char end;
long which;
which argc strtolargv &end, : ;
if which Level
std::string word word;
std::cin word word;
std::cout std::boolalpha haveSameLettersword word;
else Level
std::vector v;
std::string word;
while std::cin word
vpushbackword;
std::unorderedmap anagramGroups;
for const std::string& word : v
std::string sortedword word;
std::sortsortedword.begin sortedword.end;
anagramGroupssortedwordpushbackword;
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 ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
