| module Bob | |||||
| open System | |||||
| let containsLowercase = Seq.fold (fun a x -> if x >= 'a' && x <= 'z' then true else a) false | |||||
| let containsUppercase = Seq.fold (fun a x -> if x >= 'A' && x <= 'Z' then true else a) false | |||||
| let containsLetters input = containsLowercase input || containsUppercase input | |||||
| let lastLetter = Seq.last | |||||
| let question (input: string) = String.length input > 0 && lastLetter input = '?' | |||||
| let upperCase (input: string) = | |||||
| not (containsLowercase input) && containsUppercase input | |||||
| let emptyOrBlank (input: string) = | |||||
| String.length input = 0 | |||||
| let response (rawInput: string): string = | |||||
| let strip = ['\t';' ';'\n';'\r'] | |||||
| let input = rawInput |> Seq.filter (fun c -> not (List.contains c strip)) |> String.Concat | |||||
| let uc = upperCase input | |||||
| let q = question input | |||||
| let empty = emptyOrBlank input | |||||
| if q && uc then "Calm down, I know what I'm doing!" else | |||||
| if q then "Sure." else | |||||
| if empty then "Fine. Be that way!" else | |||||
| if uc then "Whoa, chill out!" else | |||||
| "Whatever." |
| <Project Sdk="Microsoft.NET.Sdk"> | |||||
| <PropertyGroup> | |||||
| <TargetFramework>netcoreapp3.0</TargetFramework> | |||||
| <IsPackable>false</IsPackable> | |||||
| </PropertyGroup> | |||||
| <ItemGroup> | |||||
| <Compile Include="Bob.fs" /> | |||||
| <Compile Include="BobTests.fs" /> | |||||
| </ItemGroup> | |||||
| <ItemGroup> | |||||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" /> | |||||
| <PackageReference Include="xunit" Version="2.4.1" /> | |||||
| <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" /> | |||||
| <PackageReference Include="FsUnit.xUnit" Version="3.8.1" /> | |||||
| </ItemGroup> | |||||
| </Project> |
| // This file was auto-generated based on version 1.6.0 of the canonical data. | |||||
| module BobTests | |||||
| open FsUnit.Xunit | |||||
| open Xunit | |||||
| open Bob | |||||
| [<Fact>] | |||||
| let ``Stating something`` () = | |||||
| response "Tom-ay-to, tom-aaaah-to." |> should equal "Whatever." | |||||
| [<Fact>] | |||||
| let ``Shouting`` () = | |||||
| response "WATCH OUT!" |> should equal "Whoa, chill out!" | |||||
| [<Fact>] | |||||
| let ``Shouting gibberish`` () = | |||||
| response "FCECDFCAAB" |> should equal "Whoa, chill out!" | |||||
| [<Fact>] | |||||
| let ``Asking a question`` () = | |||||
| response "Does this cryogenic chamber make me look fat?" |> should equal "Sure." | |||||
| [<Fact>] | |||||
| let ``Asking a numeric question`` () = | |||||
| response "You are, what, like 15?" |> should equal "Sure." | |||||
| [<Fact>] | |||||
| let ``Asking gibberish`` () = | |||||
| response "fffbbcbeab?" |> should equal "Sure." | |||||
| [<Fact>] | |||||
| let ``Talking forcefully`` () = | |||||
| response "Hi there!" |> should equal "Whatever." | |||||
| [<Fact>] | |||||
| let ``Using acronyms in regular speech`` () = | |||||
| response "It's OK if you don't want to go work for NASA." |> should equal "Whatever." | |||||
| [<Fact>] | |||||
| let ``Forceful question`` () = | |||||
| response "WHAT'S GOING ON?" |> should equal "Calm down, I know what I'm doing!" | |||||
| [<Fact>] | |||||
| let ``Shouting numbers`` () = | |||||
| response "1, 2, 3 GO!" |> should equal "Whoa, chill out!" | |||||
| [<Fact>] | |||||
| let ``No letters`` () = | |||||
| response "1, 2, 3" |> should equal "Whatever." | |||||
| [<Fact>] | |||||
| let ``Question with no letters`` () = | |||||
| response "4?" |> should equal "Sure." | |||||
| [<Fact>] | |||||
| let ``Shouting with special characters`` () = | |||||
| response "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!" |> should equal "Whoa, chill out!" | |||||
| [<Fact>] | |||||
| let ``Shouting with no exclamation mark`` () = | |||||
| response "I HATE THE DENTIST" |> should equal "Whoa, chill out!" | |||||
| [<Fact>] | |||||
| let ``Statement containing question mark`` () = | |||||
| response "Ending with ? means a question." |> should equal "Whatever." | |||||
| [<Fact>] | |||||
| let ``Non-letters with question`` () = | |||||
| response ":) ?" |> should equal "Sure." | |||||
| [<Fact>] | |||||
| let ``Prattling on`` () = | |||||
| response "Wait! Hang on. Are you going to be OK?" |> should equal "Sure." | |||||
| [<Fact>] | |||||
| let ``Silence`` () = | |||||
| response "" |> should equal "Fine. Be that way!" | |||||
| [<Fact>] | |||||
| let ``Prolonged silence`` () = | |||||
| response " " |> should equal "Fine. Be that way!" | |||||
| [<Fact>] | |||||
| let ``Alternate silence`` () = | |||||
| response "\t\t\t\t\t\t\t\t\t\t" |> should equal "Fine. Be that way!" | |||||
| [<Fact>] | |||||
| let ``Multiple line question`` () = | |||||
| response "\nDoes this cryogenic chamber make me look fat?\nNo." |> should equal "Whatever." | |||||
| [<Fact>] | |||||
| let ``Starting with whitespace`` () = | |||||
| response " hmmmmmmm..." |> should equal "Whatever." | |||||
| [<Fact>] | |||||
| let ``Ending with whitespace`` () = | |||||
| response "Okay if like my spacebar quite a bit? " |> should equal "Sure." | |||||
| [<Fact>] | |||||
| let ``Other whitespace`` () = | |||||
| response "\n\r \t" |> should equal "Fine. Be that way!" | |||||
| [<Fact>] | |||||
| let ``Non-question ending with whitespace`` () = | |||||
| response "This is a statement ending with whitespace " |> should equal "Whatever." | |||||
| # Bob | |||||
| Bob is a lackadaisical teenager. In conversation, his responses are very limited. | |||||
| Bob answers 'Sure.' if you ask him a question, such as "How are you?". | |||||
| He answers 'Whoa, chill out!' if you YELL AT HIM (in all capitals). | |||||
| 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. | |||||
| ## Autoformatting the code | |||||
| F# source code can be formatted with the [Fantomas](https://github.com/fsprojects/fantomas) tool. | |||||
| After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current 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](http://exercism.io/languages/fsharp/resources). | |||||
| ## Source | |||||
| Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. [http://pine.fm/LearnToProgram/?Chapter=06](http://pine.fm/LearnToProgram/?Chapter=06) | |||||