summaryrefslogtreecommitdiff
path: root/go/bob
diff options
context:
space:
mode:
Diffstat (limited to 'go/bob')
-rw-r--r--go/bob/README.md48
-rw-r--r--go/bob/bob.go32
-rw-r--r--go/bob/bob_test.go30
-rw-r--r--go/bob/cases_test.go141
4 files changed, 251 insertions, 0 deletions
diff --git a/go/bob/README.md b/go/bob/README.md
new file mode 100644
index 0000000..1ed49ff
--- /dev/null
+++ b/go/bob/README.md
@@ -0,0 +1,48 @@
+# Bob
+
+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 says 'Fine. Be that way!' if you address him without actually saying
+anything.
+
+He answers 'Whatever.' to anything else.
+
+## Instructions
+
+Run the test file, and fix each of the errors in turn. When you get the
+first test to pass, go to the first pending or skipped test, and make
+that pass as well. When all of the tests are passing, feel free to
+submit.
+
+Remember that passing code is just the first step. The goal is to work
+towards a solution that is as readable and expressive as you can make
+it.
+
+Please make your solution as general as possible. Good code doesn't just
+pass the test suite, it works with any input that fits the
+specification.
+
+Have fun!
+
+
+To run the tests simply run the command `go test` in the exercise directory.
+
+If the test suite contains benchmarks, you can run these with the `-bench`
+flag:
+
+ go test -bench .
+
+For more detailed info about the Go track see the [help
+page](http://exercism.io/languages/go).
+
+## 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)
+
+## Submitting Incomplete Problems
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
diff --git a/go/bob/bob.go b/go/bob/bob.go
new file mode 100644
index 0000000..129c0aa
--- /dev/null
+++ b/go/bob/bob.go
@@ -0,0 +1,32 @@
+package bob
+
+import (
+ "strings"
+ "unicode"
+)
+
+const testVersion = 2
+
+func Hey(s string) string {
+ s = strings.TrimSpace(s)
+ var hasUpper bool
+ var hasLower bool
+ for _, r := range s {
+ switch {
+ case unicode.IsUpper(r):
+ hasUpper = true
+ case unicode.IsLower(r):
+ hasLower = true
+ }
+ }
+ switch {
+ case hasUpper && !hasLower:
+ return "Whoa, chill out!"
+ case strings.HasSuffix(s, "?"):
+ return "Sure."
+ case len(s) == 0:
+ return "Fine. Be that way!"
+ default:
+ return "Whatever."
+ }
+}
diff --git a/go/bob/bob_test.go b/go/bob/bob_test.go
new file mode 100644
index 0000000..e53226d
--- /dev/null
+++ b/go/bob/bob_test.go
@@ -0,0 +1,30 @@
+package bob
+
+import "testing"
+
+const targetTestVersion = 2
+
+func TestHeyBob(t *testing.T) {
+ if testVersion != targetTestVersion {
+ t.Fatalf("Found testVersion = %v, want %v", testVersion, targetTestVersion)
+ }
+ 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 BenchmarkBob(b *testing.B) {
+ for _, tt := range testCases {
+ for i := 0; i < b.N; i++ {
+ Hey(tt.input)
+ }
+ }
+}
diff --git a/go/bob/cases_test.go b/go/bob/cases_test.go
new file mode 100644
index 0000000..825d11d
--- /dev/null
+++ b/go/bob/cases_test.go
@@ -0,0 +1,141 @@
+package bob
+
+// Source: exercism/x-common
+// Commit: 945d08e Merge pull request #50 from soniakeys/master
+
+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?",
+ "Whoa, chill out!",
+ },
+ {
+ "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 umlauts",
+ "ÜMLÄÜTS!",
+ "Whoa, chill out!",
+ },
+ {
+ "calmly speaking with umlauts",
+ "ÜMLäÜTS!",
+ "Whatever.",
+ },
+ {
+ "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\v\u00a0\u2002",
+ "Fine. Be that way!",
+ },
+}