summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-28 00:51:54 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-28 00:51:54 +0200
commit28978858033244093191c763dd7b8597faedae35 (patch)
tree84cb26bfd548bdaf39a943fa101bc7941badef93
parent6a5bafe3d99c26e78c4ce240eb54a917259a9861 (diff)
Solve bracket
-rw-r--r--go/bracket-push/README.md24
-rw-r--r--go/bracket-push/bracket_push.go41
-rw-r--r--go/bracket-push/bracket_push_test.go77
3 files changed, 142 insertions, 0 deletions
diff --git a/go/bracket-push/README.md b/go/bracket-push/README.md
new file mode 100644
index 0000000..e080338
--- /dev/null
+++ b/go/bracket-push/README.md
@@ -0,0 +1,24 @@
+# Bracket Push
+
+Make sure the brackets and braces all match.
+
+Ensure that all the brackets and braces are matched correctly,
+and nested correctly.
+
+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
+
+Ginna Baker
+
+## 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/bracket-push/bracket_push.go b/go/bracket-push/bracket_push.go
new file mode 100644
index 0000000..5c8d7e9
--- /dev/null
+++ b/go/bracket-push/bracket_push.go
@@ -0,0 +1,41 @@
+package brackets
+
+const testVersion = 3
+
+type Stack []rune
+
+func (s *Stack) Push(r rune) {
+ *s = append(*s, r)
+}
+
+func (s *Stack) Pop() (r rune) {
+ r, *s = (*s)[len(*s)-1], (*s)[:len(*s)-1]
+ return
+}
+
+func (s *Stack) Len() int {
+ return len(*s)
+}
+
+func Bracket(s string) (bool, error) {
+ var st Stack
+ pair := map[rune]rune{
+ '}': '{',
+ ']': '[',
+ ')': '(',
+ }
+ for _, v := range s {
+ switch v {
+ case '{', '[', '(':
+ st.Push(v)
+ case '}', ']', ')':
+ if st.Len() == 0 {
+ return false, nil
+ }
+ if r := st.Pop(); r != pair[v] {
+ return false, nil
+ }
+ }
+ }
+ return st.Len() == 0, nil
+}
diff --git a/go/bracket-push/bracket_push_test.go b/go/bracket-push/bracket_push_test.go
new file mode 100644
index 0000000..6f92877
--- /dev/null
+++ b/go/bracket-push/bracket_push_test.go
@@ -0,0 +1,77 @@
+package brackets
+
+import (
+ "testing"
+)
+
+const targetTestVersion = 3
+
+var testCases = []struct {
+ input string
+ expected bool
+}{
+ {
+ input: "",
+ expected: true,
+ },
+ {
+ input: "{}",
+ expected: true,
+ },
+ {
+ input: "{{",
+ expected: false,
+ },
+ {
+ input: "}{",
+ expected: false,
+ },
+ {
+ input: "{}[]",
+ expected: true,
+ },
+ {
+ input: "{[]}",
+ expected: true,
+ },
+ {
+ input: "{[}]",
+ expected: false,
+ },
+ {
+ input: "{[)][]}",
+ expected: false,
+ },
+ {
+ input: "{[]([()])}",
+ expected: true,
+ },
+}
+
+func TestBracket(t *testing.T) {
+ for _, tt := range testCases {
+ actual, err := Bracket(tt.input)
+ // We don't expect errors for any of the test cases
+ if err != nil {
+ t.Fatalf("Bracket(%q) returned error %q. Error not expected.", tt.input, err)
+ }
+ if actual != tt.expected {
+ t.Fatalf("Bracket(%q) was expected to return %v but returned %v.",
+ tt.input, tt.expected, actual)
+ }
+ }
+ if testVersion != targetTestVersion {
+ t.Fatalf("Found testVersion = %v, want %v.", testVersion, targetTestVersion)
+ }
+}
+
+func BenchmarkBracket(b *testing.B) {
+ b.StopTimer()
+ for _, tt := range testCases {
+ b.StartTimer()
+ for i := 0; i < b.N; i++ {
+ Bracket(tt.input)
+ }
+ b.StopTimer()
+ }
+}