Avatar of agbell

agbell's solution

to Bob in the Scala Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

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.2.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!")
  }

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

  test("asking a numeric question") {
    pending
    Bob.response("You are, what, like 15?") should be ("Sure.")
  }

  test("asking gibberish") {
    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("only numbers") {
    pending
    Bob.response("1, 2, 3") should be ("Whatever.")
  }

  test("question with only numbers") {
    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 YOU") 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.")
  }
}
class Bob {
  def hey(question : String) : String = {
    def contains(group : String) = group.r.findFirstIn(question).isDefined
    if (question.trim.isEmpty) {
      "Fine. Be that way!"
    } else if (contains("[A-Z]") && !contains("[a-z]")) {
      "Whoa, chill out!"
    } else if (question.last == '?') {
      "Sure."
    } else {
      "Whatever."
    }
  }
}

Community comments

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

you may consider using exists (instead of your contains function), isLetter, isUpper/Lower, endsWith.

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?