Avatar of Lori

Lori's solution

to Bob in the Kotlin Track

Published at Oct 13 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.

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.

Setup

Go through the setup instructions for Kotlin to install the necessary dependencies:

https://exercism.io/tracks/kotlin/installation

Making the test suite pass

Execute the tests with:

$ gradlew test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore annotation.

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.

BobTest.kt

import org.junit.Test
import org.junit.Ignore
import kotlin.test.assertEquals

class BobTest {

    @Test
    fun statingSomething() {
        assertEquals("Whatever.", Bob.hey("Tom-ay-to, tom-aaaah-to."))
    }

    @Ignore
    @Test
    fun shouting() {
        assertEquals("Whoa, chill out!", Bob.hey("WATCH OUT!"))
    }

    @Ignore
    @Test
    fun shoutingGibberish() {
        assertEquals("Whoa, chill out!", Bob.hey("FCECDFCAAB"))
    }

    @Ignore
    @Test
    fun askingAQuestion() {
        assertEquals("Sure.", Bob.hey("Does this cryogenic chamber make me look fat?"))
    }

    @Ignore
    @Test
    fun askingANumericQuestion() {
        assertEquals("Sure.", Bob.hey("You are, what, like 15?"))
    }

    @Ignore
    @Test
    fun askingGibberish() {
        assertEquals("Sure.", Bob.hey("fffbbcbeab?"))
    }

    @Ignore
    @Test
    fun talkingForcefully() {
        assertEquals("Whatever.", Bob.hey("Let's go make out behind the gym!"))
    }

    @Ignore
    @Test
    fun usingAcronymsInRegularSpeech() {
        assertEquals("Whatever.", Bob.hey("It's OK if you don't want to go to the DMV."))
    }

    @Ignore
    @Test
    fun forcefulQuestion() {
        assertEquals("Calm down, I know what I'm doing!", Bob.hey("WHAT THE HELL WERE YOU THINKING?"))
    }

    @Ignore
    @Test
    fun shoutingNumbers() {
        assertEquals("Whoa, chill out!", Bob.hey("1, 2, 3 GO!"))
    }

    @Ignore
    @Test
    fun noLetters() {
        assertEquals("Whatever.", Bob.hey("1, 2, 3"))
    }

    @Ignore
    @Test
    fun questionWithNoLetters() {
        assertEquals("Sure.", Bob.hey("4?"))
    }

    @Ignore
    @Test
    fun shoutingWithSpecialCharacters() {
        assertEquals("Whoa, chill out!", Bob.hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"))
    }

    @Ignore
    @Test
    fun shoutingWithNoExclamationMark() {
        assertEquals("Whoa, chill out!", Bob.hey("I HATE THE DMV"))
    }

    @Ignore
    @Test
    fun statementContainingQuestionMark() {
        assertEquals("Whatever.", Bob.hey("Ending with ? means a question."))
    }

    @Ignore
    @Test
    fun nonLettersWithQuestion() {
        assertEquals("Sure.", Bob.hey(":) ?"))
    }

    @Ignore
    @Test
    fun prattlingOn() {
        assertEquals("Sure.", Bob.hey("Wait! Hang on. Are you going to be OK?"))
    }

    @Ignore
    @Test
    fun silence() {
        assertEquals("Fine. Be that way!", Bob.hey(""))
    }

    @Ignore
    @Test
    fun prolongedSilence() {
        assertEquals("Fine. Be that way!", Bob.hey("          "))
    }

    @Ignore
    @Test
    fun alternateSilence() {
        assertEquals("Fine. Be that way!", Bob.hey("\t\t\t\t\t\t\t\t\t\t"))
    }

    @Ignore
    @Test
    fun multipleLineQuestion() {
        assertEquals("Whatever.", Bob.hey("\nDoes this cryogenic chamber make me look fat?\nno"))
    }

    @Ignore
    @Test
    fun startingWithWhitespace() {
        assertEquals("Whatever.", Bob.hey("         hmmmmmmm..."))
    }

    @Ignore
    @Test
    fun endingWithWhitespace() {
        assertEquals("Sure.", Bob.hey("Okay if like my  spacebar  quite a bit?   "))
    }

    @Ignore
    @Test
    fun otherWhitespace() {
        assertEquals("Fine. Be that way!", Bob.hey("\n\r \t"))
    }

    @Ignore
    @Test
    fun nonQuestionEndingWithWhitespace() {
        assertEquals("Whatever.", Bob.hey("This is a statement ending with whitespace      "))
    }

}
object Bob {

    fun hey(input:String):String {
        val sauber = input.filterNot { it.isWhitespace() }
        val ob = sauber.filter { it.isLetter() }
        return when {
            sauber.isEmpty() -> "Fine. Be that way!"
            sauber.endsWith("?") -> if (sauber.dropLast(1).all { it.isUpperCase() && it.isLetter()}) "Calm down, I know what I'm doing!"
                else "Sure."
            ob.all { it.isUpperCase()} && !ob.isEmpty() -> "Whoa, chill out!"
            else -> "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?