summaryrefslogtreecommitdiff
path: root/go/wordy/wordy.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/wordy/wordy.go')
-rw-r--r--go/wordy/wordy.go38
1 files changed, 37 insertions, 1 deletions
diff --git a/go/wordy/wordy.go b/go/wordy/wordy.go
index cfa8f1b..ec5ea35 100644
--- a/go/wordy/wordy.go
+++ b/go/wordy/wordy.go
@@ -1,5 +1,41 @@
package wordy
+import (
+ "strconv"
+ "strings"
+)
+
+type Op func(int, int) int
+
+var m = map[string]Op{
+ "plus": func(a, b int) int { return a + b },
+ "minus": func(a, b int) int { return a - b },
+ "multiplied": func(a, b int) int { return a * b },
+ "divided": func(a, b int) int { return a / b },
+}
+
func Answer(s string) (int, bool) {
- return 0, false
+ if !strings.HasPrefix(s, "What is") || !strings.HasSuffix(s, "?") {
+ return 0, false
+ }
+ var op Op
+ var x int
+ var didOp bool
+ for _, v := range strings.Fields(s[8 : len(s)-1]) {
+ if i, err := strconv.Atoi(v); err == nil {
+ // got a number
+ if op != nil {
+ x = op(x, i)
+ didOp = true
+ } else {
+ x = i
+ }
+ } else {
+ // try to fetch operation
+ if o, ok := m[v]; ok {
+ op = o
+ }
+ }
+ }
+ return x, didOp
}