Avatar of paulfioravanti

paulfioravanti's solution

to Bob in the Elixir Track

Published at Jul 13 2018 · 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 tests

Execute the tests with:

$ elixir bob_test.exs

Pending tests

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

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

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.

bob_test.exs

if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("bob.exs", __DIR__)
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule BobTest do
  use ExUnit.Case

  test "stating something" do
    assert Bob.hey("Tom-ay-to, tom-aaaah-to.") == "Whatever."
  end

  @tag :pending
  test "shouting" do
    assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
  end

  @tag :pending
  test "asking a question" do
    assert Bob.hey("Does this cryogenic chamber make me look fat?") == "Sure."
  end

  @tag :pending
  test "talking forcefully" do
    assert Bob.hey("Let's go make out behind the gym!") == "Whatever."
  end

  @tag :pending
  test "talking in capitals" do
    assert Bob.hey("This Isn't Shouting!") == "Whatever."
  end

  @tag :pending
  test "asking in capitals" do
    assert Bob.hey("THIS ISN'T SHOUTING?") == "Calm down, I know what I'm doing!"
  end

  @tag :pending
  test "shouting numbers" do
    assert Bob.hey("1, 2, 3 GO!") == "Whoa, chill out!"
  end

  @tag :pending
  test "shouting with special characters" do
    assert Bob.hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!") == "Whoa, chill out!"
  end

  @tag :pending
  test "shouting with no exclamation mark" do
    assert Bob.hey("I HATE YOU") == "Whoa, chill out!"
  end

  @tag :pending
  test "statement containing question mark" do
    assert Bob.hey("Ending with ? means a question.") == "Whatever."
  end

  @tag :pending
  test "silence" do
    assert Bob.hey("") == "Fine. Be that way!"
  end

  @tag :pending
  test "prolonged silence" do
    assert Bob.hey("  ") == "Fine. Be that way!"
  end

  @tag :pending
  test "only numbers" do
    assert Bob.hey("1, 2, 3") == "Whatever."
  end

  @tag :pending
  test "question with numbers" do
    assert Bob.hey("4?") == "Sure."
  end

  @tag :pending
  test "shouting in Russian" do
    assert Bob.hey("УХОДИ") == "Whoa, chill out!"
  end
end
defmodule Bob do
  def hey(input) do
    remark = String.trim(input)

    if silence?(remark) do
      "Fine. Be that way!"
    else
      respond_to_verbal_remark(remark)
    end
  end

  defp respond_to_verbal_remark(remark) do
    case {question?(remark), yelling?(remark)} do
      {true, true} ->
        "Calm down, I know what I'm doing!"

      {true, false} ->
        "Sure."

      {false, true} ->
        "Whoa, chill out!"

      {false, false} ->
        "Whatever."
    end
  end

  defp silence?(remark) do
    remark == ""
  end

  defp question?(remark) do
    String.ends_with?(remark, "?")
  end

  defp yelling?(remark) do
    remark == String.upcase(remark) and remark != String.downcase(remark)
  end
end

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?