🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of billwanjohi

billwanjohi's solution

to Bob in the F# Track

Published at Dec 19 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution 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.

Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Further information

For more detailed information about the F# track, including how to get help if you're having trouble, please visit the exercism.io F# language 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.fs

// This file was auto-generated based on version 1.4.0 of the canonical data.

module BobTest

open FsUnit.Xunit
open Xunit

open Bob

[<Fact>]
let ``Stating something`` () =
    response "Tom-ay-to, tom-aaaah-to." |> should equal "Whatever."

[<Fact(Skip = "Remove to run test")>]
let ``Shouting`` () =
    response "WATCH OUT!" |> should equal "Whoa, chill out!"

[<Fact(Skip = "Remove to run test")>]
let ``Shouting gibberish`` () =
    response "FCECDFCAAB" |> should equal "Whoa, chill out!"

[<Fact(Skip = "Remove to run test")>]
let ``Asking a question`` () =
    response "Does this cryogenic chamber make me look fat?" |> should equal "Sure."

[<Fact(Skip = "Remove to run test")>]
let ``Asking a numeric question`` () =
    response "You are, what, like 15?" |> should equal "Sure."

[<Fact(Skip = "Remove to run test")>]
let ``Asking gibberish`` () =
    response "fffbbcbeab?" |> should equal "Sure."

[<Fact(Skip = "Remove to run test")>]
let ``Talking forcefully`` () =
    response "Let's go make out behind the gym!" |> should equal "Whatever."

[<Fact(Skip = "Remove to run test")>]
let ``Using acronyms in regular speech`` () =
    response "It's OK if you don't want to go to the DMV." |> should equal "Whatever."

[<Fact(Skip = "Remove to run test")>]
let ``Forceful question`` () =
    response "WHAT THE HELL WERE YOU THINKING?" |> should equal "Calm down, I know what I'm doing!"

[<Fact(Skip = "Remove to run test")>]
let ``Shouting numbers`` () =
    response "1, 2, 3 GO!" |> should equal "Whoa, chill out!"

[<Fact(Skip = "Remove to run test")>]
let ``No letters`` () =
    response "1, 2, 3" |> should equal "Whatever."

[<Fact(Skip = "Remove to run test")>]
let ``Question with no letters`` () =
    response "4?" |> should equal "Sure."

[<Fact(Skip = "Remove to run test")>]
let ``Shouting with special characters`` () =
    response "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!" |> should equal "Whoa, chill out!"

[<Fact(Skip = "Remove to run test")>]
let ``Shouting with no exclamation mark`` () =
    response "I HATE THE DMV" |> should equal "Whoa, chill out!"

[<Fact(Skip = "Remove to run test")>]
let ``Statement containing question mark`` () =
    response "Ending with ? means a question." |> should equal "Whatever."

[<Fact(Skip = "Remove to run test")>]
let ``Non-letters with question`` () =
    response ":) ?" |> should equal "Sure."

[<Fact(Skip = "Remove to run test")>]
let ``Prattling on`` () =
    response "Wait! Hang on. Are you going to be OK?" |> should equal "Sure."

[<Fact(Skip = "Remove to run test")>]
let ``Silence`` () =
    response "" |> should equal "Fine. Be that way!"

[<Fact(Skip = "Remove to run test")>]
let ``Prolonged silence`` () =
    response "          " |> should equal "Fine. Be that way!"

[<Fact(Skip = "Remove to run test")>]
let ``Alternate silence`` () =
    response "\t\t\t\t\t\t\t\t\t\t" |> should equal "Fine. Be that way!"

[<Fact(Skip = "Remove to run test")>]
let ``Multiple line question`` () =
    response "\nDoes this cryogenic chamber make me look fat?\nNo." |> should equal "Whatever."

[<Fact(Skip = "Remove to run test")>]
let ``Starting with whitespace`` () =
    response "         hmmmmmmm..." |> should equal "Whatever."

[<Fact(Skip = "Remove to run test")>]
let ``Ending with whitespace`` () =
    response "Okay if like my  spacebar  quite a bit?   " |> should equal "Sure."

[<Fact(Skip = "Remove to run test")>]
let ``Other whitespace`` () =
    response "\n\r \t" |> should equal "Fine. Be that way!"

[<Fact(Skip = "Remove to run test")>]
let ``Non-question ending with whitespace`` () =
    response "This is a statement ending with whitespace      " |> should equal "Whatever."
module Bob

let (|Empty|_|) (str: string) =
    if str.Equals("") then Some(Empty) else None

let (|Shouty|_|) (str: string) =
    if str.ToUpper().Equals(str) && not (str.ToLower().Equals(str))
    then Some(Shouty)
    else None

let (|Question|_|) (str: string) =
    if str.EndsWith('?') then Some(Question) else None

let response (input: string): string =
    let trimmed = input.Trim()
    match trimmed with
    | Empty -> "Fine. Be that way!"
    | Shouty & Question -> "Calm down, I know what I'm doing!"
    | Shouty -> "Whoa, chill out!"
    | Question -> "Sure."
    | _ -> "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?