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.
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.
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.
Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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)
}
}
}
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!",
},
{
"asking a question",
"Does this cryogenic chamber make me look fat?",
"Sure.",
},
{
"asking a numeric question",
"You are, what, like 15?",
"Sure.",
},
{
"asking gibberish",
"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."
}
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.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments