Avatar of AlexLyrr

AlexLyrr's solution

to Bob in the C++ Track

Published at Nov 01 2019 · 0 comments
Instructions
Test suite
Solution

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question, such as "How are you?".

He answers 'Whoa, chill out!' if you YELL AT HIM (in all capitals).

He answers 'Calm down, I know what I'm doing!' if you yell a question at him.

He says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English.

Getting Started

Make sure you have read the Installing and Running the Tests pages for C++ on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

#if defined(EXERCISM_RUN_ALL_TESTS)

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C++14 facilities in preference to writing your own low-level algorithms or facilities by hand. CppReference is a wiki reference to the C++ language and standard library. If you are new to C++, but have programmed in C, beware of C traps and pitfalls.

Source

Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

bob_test.cpp

#include "bob.h"
#include "test/catch.hpp"

// Bob exercise test case data version 1.4.0

TEST_CASE("stating_something")
{
    REQUIRE("Whatever." == bob::hey("Tom-ay-to, tom-aaaah-to."));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("shouting")
{
    REQUIRE("Whoa, chill out!" == bob::hey("WATCH OUT!"));
}

TEST_CASE("shouting_gibberish")
{
    REQUIRE("Whoa, chill out!" == bob::hey("FCECDFCAAB"));
}

TEST_CASE("asking_a_question")
{
    REQUIRE("Sure." == bob::hey("Does this cryogenic chamber make me look fat?"));
}

TEST_CASE("asking_a_numeric_question")
{
    REQUIRE("Sure." == bob::hey("You are, what, like 15?"));
}

TEST_CASE("asking_gibberish")
{
    REQUIRE("Sure." == bob::hey("fffbbcbeab?"));
}

TEST_CASE("talking_forcefully")
{
    REQUIRE("Whatever." == bob::hey("Let's go make out behind the gym!"));
}

TEST_CASE("using_acronyms_in_regular_speech")
{
    REQUIRE("Whatever." == bob::hey("It's OK if you don't want to go to the DMV."));
}

TEST_CASE("forceful_questions")
{
    REQUIRE("Calm down, I know what I'm doing!" == bob::hey("WHAT THE HELL WERE YOU THINKING?"));
}

TEST_CASE("shouting_numbers")
{
    REQUIRE("Whoa, chill out!" == bob::hey("1, 2, 3 GO!"));
}

TEST_CASE("no_letters")
{
    REQUIRE("Whatever." == bob::hey("1, 2, 3"));
}

TEST_CASE("question_with_no_letters")
{
    REQUIRE("Sure." == bob::hey("4?"));
}

TEST_CASE("shouting_with_special_characters")
{
    REQUIRE("Whoa, chill out!" == bob::hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"));
}

TEST_CASE("shouting_with_no_exclamation_mark")
{
    REQUIRE("Whoa, chill out!" == bob::hey("I HATE THE DMV"));
}

TEST_CASE("statement_containing_question_mark")
{
    REQUIRE("Whatever." == bob::hey("Ending with ? means a question."));
}

TEST_CASE("non_letters_with_question")
{
    REQUIRE("Sure." == bob::hey(":) ?"));
}

TEST_CASE("prattling_on")
{
    REQUIRE("Sure." == bob::hey("Wait! Hang on. Are you going to be OK?"));
}

TEST_CASE("silence")
{
    REQUIRE("Fine. Be that way!" == bob::hey(""));
}

TEST_CASE("prolonged_silence")
{
    REQUIRE("Fine. Be that way!" == bob::hey("          "));
}

TEST_CASE("alternate_silence")
{
    REQUIRE("Fine. Be that way!" == bob::hey("\t\t\t\t\t\t\t\t\t\t"));
}

TEST_CASE("multiple_line_question")
{
	REQUIRE("Whatever." == bob::hey("\nDoes this cryogenic chamber make me look fat?\nNo."));
}

TEST_CASE("starting_with_whitespace")
{
	REQUIRE("Whatever." == bob::hey("         hmmmmmmm..."));
}

TEST_CASE("ending_with_whitespace")
{
	REQUIRE("Sure." == bob::hey("Okay if like my  spacebar  quite a bit?   "));
}

TEST_CASE("other_whitespace")
{
	REQUIRE("Fine. Be that way!" == bob::hey("\n\r \t"));
}

TEST_CASE("non_question_ending_with_whitespace")
{
	REQUIRE("Whatever." == bob::hey("This is a statement ending with whitespace      "));
}
#endif

bob.h

#if !defined(BOB_H)
#define BOB_H

#include <string>

namespace bob {
    std::string hey(const std::string &input);
} // namespace bob

#endif // BOB_H

bob.cpp

#include "bob.h"

#include <algorithm>
#include <cctype>

namespace bob {
    std::string hey(const std::string &input) {
        bool finishWithQuestionMark = false;
        bool allCaps = false;
        bool allNonPrintableCharacters = false;
        if (std::all_of(input.begin() + input.find_last_of('?') + 1, input.end(), [](char c) { return c == ' '; })) {
            finishWithQuestionMark = true;
        }
        if (std::all_of(input.begin(), input.end(), [](char c) { return !islower(c); }) &&
            std::any_of(input.begin(), input.end(), [](char c) { return isupper(c); })) {
            allCaps = true;
        }
        if (std::all_of(input.begin(), input.end(), [](char c) { return !isprint(c) || c == ' '; })) {
            allNonPrintableCharacters = true;
        }

        if (allNonPrintableCharacters) {
            return "Fine. Be that way!";
        } else if (allCaps && finishWithQuestionMark) {
            return "Calm down, I know what I'm doing!";
        } else if (finishWithQuestionMark) {
            return "Sure.";
        } else if (allCaps) {
            return "Whoa, chill out!";
        } else {
            return "Whatever.";
        }
    }

} // namespace bob

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?