summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-11-11 14:11:51 +0100
committerDimitri Sokolyuk <demon@dim13.org>2016-11-11 14:11:51 +0100
commita330b3817e89369f34793d52003252506454dfe9 (patch)
tree2255d2683e8477037245d2119774c957b46e9150
parent5692e2217c6074504884ad2b3c64d555452cc7f2 (diff)
Add wordy
-rw-r--r--go/wordy/README.md89
-rw-r--r--go/wordy/wordy_test.go50
2 files changed, 139 insertions, 0 deletions
diff --git a/go/wordy/README.md b/go/wordy/README.md
new file mode 100644
index 0000000..09c35b6
--- /dev/null
+++ b/go/wordy/README.md
@@ -0,0 +1,89 @@
+# Wordy
+
+Write a program that takes a word problem and returns the answer as an integer.
+
+## Step 1
+
+E.g.
+
+> What is 5 plus 13?
+
+The program should handle large numbers and negative numbers.
+
+Remember, that these are verbal word problems, not treated as you
+normally would treat a written problem. This means that you calculate
+as you move forward each step. In other words, you should ignore order
+of operations. 3 + 2 * 3 = 15, not 9.
+
+Use the tests to drive your solution by deleting the `skip` in one test
+at a time.
+
+## Step 2
+
+E.g.
+
+> What is 5 plus 13?
+
+> What is 7 minus 5?
+
+> What is 6 multiplied by 4?
+
+> What is 25 divided by 5?
+
+## Step 3
+
+E.g.
+
+> What is 5 plus 13 plus 6?
+
+> What is 7 minus 5 minus 1?
+
+> What is 9 minus 3 plus 5?
+
+> What is 3 plus 5 minus 8?
+
+## Step 4
+
+E.g.
+
+> What is 5 plus 13?
+
+> What is 7 minus 5?
+
+> What is 6 times 4?
+
+> What is 25 divided by 5?
+
+> What is 78 plus 5 minus 3?
+
+> What is 18 times 3 plus 16?
+
+> What is 4 times 3 divided by 6?
+
+> What is 4 plus 3 times 2?
+
+## Extensions
+
+Implement questions of the type:
+
+> What is 2 raised to the 5th power?
+
+Remember to write failing tests for this code.
+
+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 one of the generated questions in the Extreme Startup game. [https://github.com/rchatley/extreme_startup](https://github.com/rchatley/extreme_startup)
+
+## 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/wordy/wordy_test.go b/go/wordy/wordy_test.go
new file mode 100644
index 0000000..885625e
--- /dev/null
+++ b/go/wordy/wordy_test.go
@@ -0,0 +1,50 @@
+package wordy
+
+import "testing"
+
+var tests = []struct {
+ q string
+ a int
+ ok bool
+}{
+ {"What is 1 plus 1?", 2, true},
+ {"What is 53 plus 2?", 55, true},
+ {"What is -1 plus -10?", -11, true},
+ {"What is 123 plus 45678?", 45801, true},
+ {"What is 4 minus -12?", 16, true},
+ {"What is -3 multiplied by 25?", -75, true},
+ {"What is 33 divided by -3?", -11, true},
+ {"What is 1 plus 1 plus 1?", 3, true},
+ {"What is 1 plus 5 minus -2?", 8, true},
+ {"What is 20 minus 4 minus 13?", 3, true},
+ {"What is 17 minus 6 plus 3?", 14, true},
+ {"What is 2 multiplied by -2 multiplied by 3?", -12, true},
+ {"What is -3 plus 7 multiplied by -2?", -8, true},
+ {"What is -12 divided by 2 divided by -3?", 2, true},
+ {"What is 53 cubed?", 0, false},
+ {"Who is the president of the United States?", 0, false},
+}
+
+func TestAnswer(t *testing.T) {
+ for _, test := range tests {
+ switch a, ok := Answer(test.q); {
+ case !ok:
+ if test.ok {
+ t.Errorf("Answer(%q) returned ok = false, expecting true.", test.q)
+ }
+ case !test.ok:
+ t.Errorf("Answer(%q) = %d, %t, expecting ok = false.", test.q, a, ok)
+ case a != test.a:
+ t.Errorf("Answer(%q) = %d, want %d.", test.q, a, test.a)
+ }
+ }
+}
+
+// Benchmark combined time to answer all questions.
+func BenchmarkAnswer(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ for _, test := range tests {
+ Answer(test.q)
+ }
+ }
+}