artemkorsakov's solution

to Bob in the Go Track

Published at Jan 31 2019 · 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 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: ca79943 Bob: Fix grammatical error in testdata (#1319)
// Problem Specifications Version: 1.4.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.",
},
{
"asking a numeric question",
"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!",
},
{
"no letters",
"1, 2, 3",
"Whatever.",
},
{
"question with no letters",
"4?",
"Sure.",
},
{
"shouting with special characters",
"ZOMG THE %^*@#\$(*^ ZOMBIES ARE COMING!!11!!1!",
"Whoa, chill out!",
},
{
"shouting with no exclamation mark",
"I HATE THE DMV",
"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.",
},
}``````
``````package bob

import (
"regexp"
"strings"
)

func Hey(remark string) string {
var remarkWithoutWhitespaces = regexp.MustCompile("(\\s+)").ReplaceAllString(remark, "");
if len(remarkWithoutWhitespaces) == 0 {
return "Fine. Be that way!"
}
var isQuestion = strings.HasSuffix(remarkWithoutWhitespaces, "?")
var isYell = (remarkWithoutWhitespaces == strings.ToUpper(remarkWithoutWhitespaces)) && (regexp.MustCompile(".*([A-Z]+).*").MatchString(remarkWithoutWhitespaces))
if isQuestion && isYell {
return "Calm down, I know what I'm doing!"
}
if isQuestion {
return "Sure."
}
if isYell {
return "Whoa, chill out!"
}
return "Whatever."
}``````