ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# billwanjohi's solution

## to Bob in the Scala Track

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

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

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.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

## 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.scala

``````import org.scalatest.{Matchers, FunSuite}

/** @version 1.4.0 */
class BobTest extends FunSuite with Matchers {

test("stating something") {
Bob.response("Tom-ay-to, tom-aaaah-to.") should be("Whatever.")
}

test("shouting") {
pending
Bob.response("WATCH OUT!") should be("Whoa, chill out!")
}

test("shouting gibberish") {
pending
Bob.response("FCECDFCAAB") should be("Whoa, chill out!")
}

pending
Bob.response("Does this cryogenic chamber make me look fat?") should be(
"Sure.")
}

pending
Bob.response("You are, what, like 15?") should be("Sure.")
}

pending
Bob.response("fffbbcbeab?") should be("Sure.")
}

test("talking forcefully") {
pending
Bob.response("Let's go make out behind the gym!") should be("Whatever.")
}

test("using acronyms in regular speech") {
pending
Bob.response("It's OK if you don't want to go to the DMV.") should be(
"Whatever.")
}

test("forceful question") {
pending
Bob.response("WHAT THE HELL WERE YOU THINKING?") should be(
"Calm down, I know what I'm doing!")
}

test("shouting numbers") {
pending
Bob.response("1, 2, 3 GO!") should be("Whoa, chill out!")
}

test("no letters") {
pending
Bob.response("1, 2, 3") should be("Whatever.")
}

test("question with no letters") {
pending
Bob.response("4?") should be("Sure.")
}

test("shouting with special characters") {
pending
Bob.response("ZOMG THE %^*@#\$(*^ ZOMBIES ARE COMING!!11!!1!") should be(
"Whoa, chill out!")
}

test("shouting with no exclamation mark") {
pending
Bob.response("I HATE THE DMV") should be("Whoa, chill out!")
}

test("statement containing question mark") {
pending
Bob.response("Ending with ? means a question.") should be("Whatever.")
}

test("non-letters with question") {
pending
Bob.response(":) ?") should be("Sure.")
}

test("prattling on") {
pending
Bob.response("Wait! Hang on. Are you going to be OK?") should be("Sure.")
}

test("silence") {
pending
Bob.response("") should be("Fine. Be that way!")
}

test("prolonged silence") {
pending
Bob.response("          ") should be("Fine. Be that way!")
}

test("alternate silence") {
pending
Bob.response("										") should be("Fine. Be that way!")
}

test("multiple line question") {
pending
Bob.response("""
Does this cryogenic chamber make me look fat?
No.""") should be("Whatever.")
}

test("starting with whitespace") {
pending
Bob.response("         hmmmmmmm...") should be("Whatever.")
}

test("ending with whitespace") {
pending
Bob.response("Okay if like my  spacebar  quite a bit?   ") should be(
"Sure.")
}

test("other whitespace") {
pending
Bob.response("""
""") should be("Fine. Be that way!")
}

test("non-question ending with whitespace") {
pending
Bob.response("This is a statement ending with whitespace      ") should be(
"Whatever.")
}
}``````
``````import scala.collection.GenSeq

object Empty {
/**
* Taking advantage of extractor objects to
* express patterns separately
*/
def unapply(str: String) = {
if (str == "") Some(str) else None
}
}

object Shouty {
def unapply(str: String) = {
val hasChars = (str: String, x: GenSeq[Char]) =>
!str.intersect(x).isEmpty
val hasUpper = (str: String) =>
hasChars(str, 'A' to 'Z')
val hasNoLower = (str: String) =>
!hasChars(str, 'a' to 'z')
if (hasUpper(str) && hasNoLower(str))
Some(str) else None
}
}

object Question {
def unapply(str: String) = {
if (str.endsWith("?")) Some(str) else None
}
}

object Bob {
def response(statement: String): String = {
val trimmed = statement.trim()
trimmed match {
case Empty(_) => "Fine. Be that way!"
// I wanted to do something like:
// `case Shouty(_) & Question(_)`
case Shouty(x) if x.endsWith("?") =>
"Calm down, I know what I'm doing!"
case Shouty(_) => "Whoa, chill out!"
case Question(_) => "Sure."
case _ => "Whatever."
}
}
}``````