Initial Commit
This commit is contained in:
20
go/bracket-push/README.md
Normal file
20
go/bracket-push/README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# 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://help.exercism.io/getting-started-with-go.html).
|
||||
|
||||
## Source
|
||||
|
||||
Ginna Baker [view source]()
|
26
go/bracket-push/bracket-push.go
Normal file
26
go/bracket-push/bracket-push.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package bracket_push
|
||||
|
||||
// Bracket makes sure that all brackets match
|
||||
func Bracket(s string) (bool, error) {
|
||||
// Set up a map of matches that we care about
|
||||
matches := make(map[byte]byte)
|
||||
matches['}'] = '{'
|
||||
matches[')'] = '('
|
||||
matches[']'] = '['
|
||||
var pairs []byte
|
||||
for i := range s {
|
||||
if v, ok := matches[s[i]]; ok {
|
||||
// Found a match, it's a closing bracket
|
||||
if len(pairs) == 0 || pairs[len(pairs)-1] != v {
|
||||
return false, nil
|
||||
}
|
||||
pairs = pairs[:len(pairs)-1]
|
||||
} else {
|
||||
pairs = append(pairs, s[i])
|
||||
}
|
||||
}
|
||||
if len(pairs) > 0 {
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
72
go/bracket-push/bracket-push_test.go
Normal file
72
go/bracket-push/bracket-push_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package bracket_push
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
77
go/bracket-push/bracket_push_test.go
Normal file
77
go/bracket-push/bracket_push_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package brackets
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
const testVersion = 2
|
||||
|
||||
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 != testVersion {
|
||||
t.Fatalf("Found TestVersion = %v, want %v.", TestVersion, testVersion)
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user