LinkedInLearning / cpp-esst2-2499264

C++ Essential Training 2
Other
1 stars 6 forks source link

Bug/Enhancement for challenge 4: GroupingWords #1

Open DavidKroeter84 opened 1 year ago

DavidKroeter84 commented 1 year ago

Hi!

Your current solution for the challenge in chapter 4 "Grouping Words" works totally fine if the group of words only contain unique letters (i.e. each letter is only available one single time), such as

{"eat","tea","tan","ate","nat","bat"}

However, if the group looks like the following

{"eat","tea","tan","ate","nat","bat","baat"}

The result would be wrong:

Group abt:
bat baat
Group aet:
eat tea ate
Group ant:
tan nat

The expected result would be:

Group aabt:
baat
Group abt:
bat
Group aet:
eat tea ate
Group ant:
tan nat

A proposal to overcome the problematic behaviour is to change your lambda function "wordToKey" to the following (remember to include at the top of the source file):

auto wordToKey = [](const std::string &word) -> std::string {
    std::string result(word);
    std::sort(result.begin(), result.end());

    return result;
};
gerstrong commented 1 year ago

Hi @DavidKroeter84 : Thanks for hinting this one out. You are totally right. With examples like yours mentioned it does not work and your proposal with std::sort solves the problem.

The only issue I have is that the challenge shall focus on std::set and std::maps, which is missing with your proposal. I will think about maybe coming up with a better challenge.

Maybe we should talk about a permutation in this one.

BR Gerhard