🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of n0mn0m

n0mn0m's solution

to Bob in the C# Track

Published at Nov 08 2020 · 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.

He answers 'Whoa, chill out!' if you yell at him.

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.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit Bob.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

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

BobTests.cs

// This file was auto-generated based on version 1.6.0 of the canonical data.

using Xunit;

public class BobTests
{
    [Fact]
    public void Stating_something()
    {
        Assert.Equal("Whatever.", Bob.Response("Tom-ay-to, tom-aaaah-to."));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Shouting()
    {
        Assert.Equal("Whoa, chill out!", Bob.Response("WATCH OUT!"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Shouting_gibberish()
    {
        Assert.Equal("Whoa, chill out!", Bob.Response("FCECDFCAAB"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Asking_a_question()
    {
        Assert.Equal("Sure.", Bob.Response("Does this cryogenic chamber make me look fat?"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Asking_a_numeric_question()
    {
        Assert.Equal("Sure.", Bob.Response("You are, what, like 15?"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Asking_gibberish()
    {
        Assert.Equal("Sure.", Bob.Response("fffbbcbeab?"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Talking_forcefully()
    {
        Assert.Equal("Whatever.", Bob.Response("Hi there!"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Using_acronyms_in_regular_speech()
    {
        Assert.Equal("Whatever.", Bob.Response("It's OK if you don't want to go work for NASA."));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Forceful_question()
    {
        Assert.Equal("Calm down, I know what I'm doing!", Bob.Response("WHAT'S GOING ON?"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Shouting_numbers()
    {
        Assert.Equal("Whoa, chill out!", Bob.Response("1, 2, 3 GO!"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void No_letters()
    {
        Assert.Equal("Whatever.", Bob.Response("1, 2, 3"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Question_with_no_letters()
    {
        Assert.Equal("Sure.", Bob.Response("4?"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Shouting_with_special_characters()
    {
        Assert.Equal("Whoa, chill out!", Bob.Response("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Shouting_with_no_exclamation_mark()
    {
        Assert.Equal("Whoa, chill out!", Bob.Response("I HATE THE DENTIST"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Statement_containing_question_mark()
    {
        Assert.Equal("Whatever.", Bob.Response("Ending with ? means a question."));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Non_letters_with_question()
    {
        Assert.Equal("Sure.", Bob.Response(":) ?"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Prattling_on()
    {
        Assert.Equal("Sure.", Bob.Response("Wait! Hang on. Are you going to be OK?"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Silence()
    {
        Assert.Equal("Fine. Be that way!", Bob.Response(""));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Prolonged_silence()
    {
        Assert.Equal("Fine. Be that way!", Bob.Response("          "));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Alternate_silence()
    {
        Assert.Equal("Fine. Be that way!", Bob.Response("\t\t\t\t\t\t\t\t\t\t"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Multiple_line_question()
    {
        Assert.Equal("Whatever.", Bob.Response("\nDoes this cryogenic chamber make me look fat?\nNo."));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Starting_with_whitespace()
    {
        Assert.Equal("Whatever.", Bob.Response("         hmmmmmmm..."));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Ending_with_whitespace()
    {
        Assert.Equal("Sure.", Bob.Response("Okay if like my  spacebar  quite a bit?   "));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Other_whitespace()
    {
        Assert.Equal("Fine. Be that way!", Bob.Response("\n\r \t"));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Non_question_ending_with_whitespace()
    {
        Assert.Equal("Whatever.", Bob.Response("This is a statement ending with whitespace      "));
    }
}
using System.Linq;

public static class Bob
{
    private static bool Silence(this string input) => string.IsNullOrWhiteSpace(input);
    private static bool Shouting(this string input) => input.Any(char.IsLetter) && input.ToUpper() == input;
    private static bool Question(this string input) => input.TrimEnd().EndsWith("?");

    public static string Response(string message) => message switch
    {
        _ when message.Silence() => "Fine. Be that way!",
        _ when message.Shouting() && message.Question() => "Calm down, I know what I'm doing!",
        _ when message.Shouting() => "Whoa, chill out!",
        _ when message.Question() => "Sure.",
        _ => "Whatever."
    };
}

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?