Skip to content

Bob

Answer a remark the way a lackadaisical teenager would, depending on whether it is a question, a shout, both, or silence.

Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.

// FILE: bob.nomi
//# Bob, a lackadaisical teenager, and how he answers what is said to him.

/// Bob's reply to `remark`. He answers a question, a shout, a shouted
/// question and silence each in his own way, and anything else with
/// "Whatever."
pub fn response(remark: String) -> String
    remark = String.trim(remark)
    if remark == "" then return "Fine. Be that way!"

    case (String.ends_with(remark, "?"), String.is_upper(remark))
        (True, True)   -> "Calm down, I know what I'm doing!"
        (True, False)  -> "Sure."
        (False, True)  -> "Whoa, chill out!"
        (False, False) -> "Whatever."
    end
end

// FILE: bob_test.nomi
import bob.response

test "bob's responses"
    [
        ("asking a question", "Does this cryogenic chamber make me look fat?", "Sure."),
        ("shouting", "WATCH OUT!", "Whoa, chill out!"),
        ("forceful question", "WHAT'S GOING ON?", "Calm down, I know what I'm doing!"),
        ("silence", "", "Fine. Be that way!"),
        ("stating something", "Tom-ay-to, tom-aaaah-to.", "Whatever."),
        ("asking a numeric question", "You are, what, like 15?", "Sure."),
        ("asking gibberish", "fffbbcbeab?", "Sure."),
        ("question with no letters", "4?", "Sure."),
        ("non-letters with question", ":) ?", "Sure."),
        ("prattling on", "Wait! Hang on. Are you going to be OK?", "Sure."),
        ("ending with whitespace", "Okay if like my  spacebar  quite a bit?   ", "Sure."),
        ("multiple line question", "\nDoes this cryogenic chamber make\n me look fat?", "Sure."),
        ("shouting gibberish", "FCECDFCAAB", "Whoa, chill out!"),
        (
            "shouting a statement containing a question mark",
            "DO LIONS EAT PEOPLE? AHHHHH.",
            "Whoa, chill out!",
        ),
        ("shouting numbers", "1, 2, 3 GO!", "Whoa, chill out!"),
        (
            "shouting with special characters",
            "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!",
            "Whoa, chill out!",
        ),
        ("shouting with no exclamation mark", "I HATE THE DENTIST",   "Whoa, chill out!"),
        ("prolonged silence",                 "          ",           "Fine. Be that way!"),
        ("alternate silence",                 "\t\t\t\t\t\t\t\t\t\t", "Fine. Be that way!"),
        ("other whitespace",                  "\n\r \t",              "Fine. Be that way!"),
        ("talking forcefully",                "Hi there!",            "Whatever."),
        (
            "using acronyms in regular speech",
            "It's OK if you don't want to go work for NASA.",
            "Whatever.",
        ),
        ("no letters",                         "1, 2, 3",                         "Whatever."),
        ("statement containing question mark", "Ending with ? means a question.", "Whatever."),
        ("starting with whitespace",           "         hmmmmmmm...",            "Whatever."),
        (
            "non-question ending with whitespace",
            "This is a statement ending with whitespace      ",
            "Whatever.",
        ),
    ]
    |> subtests (description, remark, expected), description
        assert response(remark) == expected
    end
end
  • Silence leaves early with if remark == "" { return ... }, and the other four answers are a case on the tuple (question, shouting) with all four Bool pairs written out. The checker proves the four arms exhaustive, and the table reads as the problem’s rules. A chain of else ifs was the alternative.
  • A shout is String.is_upper(remark): the remark has a letter, and every letter is upper case. That is the problem’s rule in one call. Comparing the remark with String.uppercase of it was the alternative, but "1, 2, 3" equals its own upper case, so that needs a second check for a letter.
  • The parameter is trimmed into a binding of the same name (remark = String.trim(remark)), per style §6.
  • Replies are plain Strings, since the canonical data compares strings and nothing else consumes them.
  • The “other whitespace” case writes its remark "\n\r \t", the escapes the canonical data uses, so the test reads as the specification does.
  • The case “multiple line question” (uuid 66953780) is left out because canonical data replaces it with uuid 2c7278ac, which is included under the same description.