Avatar of beirigo

beirigo's solution

to Bob in the Elixir Track

Published at Jul 11 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 tests

Execute the tests with:

$ mix test

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

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 "shouting gibberish" do
    assert Bob.hey("FCECDFCAAB") == "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 "asking a numeric question" do
    assert Bob.hey("You are, what, like 15?") == "Sure."
  end

  @tag :pending
  test "asking gibberish" do
    assert Bob.hey("fffbbcbeab?") == "Sure."
  end

  @tag :pending
  test "talking forcefully" do
    assert Bob.hey("Hi there!") == "Whatever."
  end

  @tag :pending
  test "using acronyms in regular speech" do
    assert Bob.hey("It's OK if you don't want to go to the DMV.") == "Whatever."
  end

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

  @tag :pending
  test "forceful question" do
    assert Bob.hey("WHAT'S GOING ON?") == "Calm down, I know what I'm doing!"
  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 THE DENTIST") == "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 "alternate silence" do
    assert Bob.hey("\t\t\t\t\t\t\t\t\t\t") == "Fine. Be that way!"
  end

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

  @tag :pending
  test "multiple line question" do
    assert Bob.hey("\nDoes this cryogenic chamber make me look fat?\nNo.") == "Whatever."
  end

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

  @tag :pending
  test "non-letters with question" do
    assert Bob.hey(":) ?") == "Sure."
  end

  @tag :pending
  test "prattling on" do
    assert Bob.hey("Wait! Hang on. Are you going to be OK?") == "Sure."
  end

  @tag :pending
  test "starting with whitespace" do
    assert Bob.hey("         hmmmmmmm...") == "Whatever."
  end

  @tag :pending
  test "ending with whitespace" do
    assert Bob.hey("Okay if like my  spacebar  quite a bit?   ") == "Sure."
  end

  @tag :pending
  test "other whitespace" do
    assert Bob.hey("\n\r \t") == "Fine. Be that way!"
  end

  @tag :pending
  test "non-question ending with whitespace" do
    assert Bob.hey("This is a statement ending with whitespace     ") == "Whatever."
  end

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

test_helper.exs

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule Bob do
  def hey(input) do
    cond do
      silence?(input) -> "Fine. Be that way!"
      forceful_question?(input) -> "Calm down, I know what I'm doing!"
      question?(input) -> "Sure."
      yelling?(input) -> "Whoa, chill out!"
      true -> "Whatever."
    end
  end

  defp question?(input), do: input |> String.trim() |> String.ends_with?("?")
  defp forceful_question?(input), do: question?(input) && yelling?(input)
  defp silence?(input), do: String.trim(input) == ""

  defp yelling?(input) do
    String.upcase(input) == input && String.downcase(input) != input
  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?