ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# nicolemon's solution

## to Bob in the Go Track

Published at Jul 13 2018 · 0 comments
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.

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.

## Running the tests

To run the tests run the command `go test` from within the exercise directory.

If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem` flags:

``````go test -v --bench . --benchmem
``````

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

## Further information

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

### bob_test.go

``````package bob

import "testing"

func TestHey(t *testing.T) {
for _, tt := range testCases {
actual := Hey(tt.input)
if actual != tt.expected {
msg := `
ALICE (%s): %q
BOB: %s

Expected Bob to respond: %s`
t.Fatalf(msg, tt.description, tt.input, actual, tt.expected)
}
}
}

func BenchmarkHey(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tt := range testCases {
Hey(tt.input)
}
}
}``````

### cases_test.go

``````package bob

// Source: exercism/problem-specifications
// Commit: 6dc2014 bob: apply "input" policy
// Problem Specifications Version: 1.2.0

var testCases = []struct {
description string
input       string
expected    string
}{
{
"stating something",
"Tom-ay-to, tom-aaaah-to.",
"Whatever.",
},
{
"shouting",
"WATCH OUT!",
"Whoa, chill out!",
},
{
"shouting gibberish",
"FCECDFCAAB",
"Whoa, chill out!",
},
{
"Does this cryogenic chamber make me look fat?",
"Sure.",
},
{
"You are, what, like 15?",
"Sure.",
},
{
"fffbbcbeab?",
"Sure.",
},
{
"talking forcefully",
"Let's go make out behind the gym!",
"Whatever.",
},
{
"using acronyms in regular speech",
"It's OK if you don't want to go to the DMV.",
"Whatever.",
},
{
"forceful question",
"WHAT THE HELL WERE YOU THINKING?",
"Calm down, I know what I'm doing!",
},
{
"shouting numbers",
"1, 2, 3 GO!",
"Whoa, chill out!",
},
{
"only numbers",
"1, 2, 3",
"Whatever.",
},
{
"question with only numbers",
"4?",
"Sure.",
},
{
"shouting with special characters",
"ZOMG THE %^*@#\$(*^ ZOMBIES ARE COMING!!11!!1!",
"Whoa, chill out!",
},
{
"shouting with no exclamation mark",
"I HATE YOU",
"Whoa, chill out!",
},
{
"statement containing question mark",
"Ending with ? means a question.",
"Whatever.",
},
{
"non-letters with question",
":) ?",
"Sure.",
},
{
"prattling on",
"Wait! Hang on. Are you going to be OK?",
"Sure.",
},
{
"silence",
"",
"Fine. Be that way!",
},
{
"prolonged silence",
"          ",
"Fine. Be that way!",
},
{
"alternate silence",
"\t\t\t\t\t\t\t\t\t\t",
"Fine. Be that way!",
},
{
"multiple line question",
"\nDoes this cryogenic chamber make me look fat?\nno",
"Whatever.",
},
{
"starting with whitespace",
"         hmmmmmmm...",
"Whatever.",
},
{
"ending with whitespace",
"Okay if like my  spacebar  quite a bit?   ",
"Sure.",
},
{
"other whitespace",
"\n\r \t",
"Fine. Be that way!",
},
{
"non-question ending with whitespace",
"This is a statement ending with whitespace      ",
"Whatever.",
},
}``````
``````// Solution to exercism/go/bob
package bob

import "strings"
import "regexp"

// Returns true if remark ends with a question mark
func isQuestion(remark string) bool {
return strings.HasSuffix(remark, "?")
}

// Returns true if all alpha characters are capitalized
func isYelling(remark string) bool {
alphas := regexp.MustCompile("[[:alpha:]]")
uppers := regexp.MustCompile("[[:upper:]]")
remarkAlphas := alphas.FindAllString(remark, -1)
remarkUppers := uppers.FindAllString(remark, -1)
return (len(remarkUppers) > 0) && (len(remarkUppers) == len(remarkAlphas))
}

// Returns true if remark contains nothing
func isSilent(remark string) bool {
return strings.Compare("", remark) == 0
}

// Responds accordingly
func Hey(remark string) string {
s := strings.TrimSpace(remark)
switch {
case isQuestion(s) && isYelling(s):
return "Calm down, I know what I'm doing!"
case isQuestion(s):
return "Sure."
case isYelling(s):
return "Whoa, chill out!"
case isSilent(s):
return "Fine. Be that way!"
}
return "Whatever."
}``````