Skip to content

Instantly share code, notes, and snippets.

@wingyplus
Last active March 25, 2018 15:23
Show Gist options
  • Save wingyplus/24cc825bf9d731e1962900ae26162eb7 to your computer and use it in GitHub Desktop.
Save wingyplus/24cc825bf9d731e1962900ae26162eb7 to your computer and use it in GitHub Desktop.
$ git diff
diff --git a/check.go b/check.go
index 6a25548..485f92e 100644
--- a/check.go
+++ b/check.go
@@ -2,6 +2,10 @@ package main
func check(input string) bool {
for i := 0; i < len(input); i++ {
+ if input[len(input)-1] == '{' {
+ return false
+ }
+
b := input[i]
if b == 'x' {
@@ -9,9 +13,6 @@ func check(input string) bool {
}
if b == '{' {
- if i == len(input)-1 {
- return false
- }
if input[i+1] == 'x' {
return false
}
$ benchcmp before.out after.out
benchmark old ns/op new ns/op delta
BenchmarkCheck/xxxxxxxxxxx{}xxxxxxxxxxxxxxx-8 21.7 29.5 +35.94%
BenchmarkCheck/xxxxxxxxxxx{}xxxxxxx{}xxxxx-8 21.2 27.9 +31.60%
BenchmarkCheck/xxxxxxxxxxxxx-8 12.3 15.7 +27.64%
BenchmarkCheck/xxxxxxxxxxx{xxxxxxx{}xxxxx-8 10.9 14.0 +28.44%
BenchmarkCheck/xxxxxxxxxxx{}xxxxxxx{x}xxxxx-8 17.1 22.8 +33.33%
BenchmarkCheck/xxxxxxxxxxx{x}xxxxxxxxxxxxxxx-8 11.3 14.1 +24.78%
BenchmarkCheck/xxxxxxxxxxx}{xxxxxxxxxxxxxxx-8 11.5 14.3 +24.35%
BenchmarkCheck/{xxxxxxxxxxxxxxx-8 3.87 3.58 -7.49%
BenchmarkCheck/xxxxxxxxxxxxxxx}x-8 13.8 18.1 +31.16%
BenchmarkCheck/xxxxxxxxxx{-8 10.3 3.14 -69.51%
BenchmarkCheck/}xxxxxxxxxx-8 3.96 3.72 -6.06%
BenchmarkCheck/x{}xxx{xx-8 8.96 10.0 +11.61%
$
package main
func check(input string) bool {
if input[len(input)-1] == '{' || input[0] == '}' {
return false
}
for i := 0; i < len(input); i++ {
b := input[i]
if b == 'x' {
continue
}
if b == '{' {
if input[i+1] == 'x' || input[i+1] == '{' {
return false
}
i++
continue
}
if b == '}' {
if input[i-1] == '{' {
continue
}
return false
}
}
return true
}
package main
import "testing"
var tcs = []struct {
input string
output bool
}{
{"xxxxxxxxxxx{}xxxxxxxxxxxxxxx", true},
{"xxxxxxxxxxx{}xxxxxxx{}xxxxx", true},
{"xxxxxxxxxxxxx", true},
{"xxxxxxxxxxx{xxxxxxx{}xxxxx", false},
{"xxxxxxxxxxx{}xxxxxxx{x}xxxxx", false},
{"xxxxxxxxxxx{x}xxxxxxxxxxxxxxx", false},
{"xxxxxxxxxxx}{xxxxxxxxxxxxxxx", false},
{"{xxxxxxxxxxxxxxx", false},
{"xxxxxxxxxxxxxxx}x", false},
{"xxxxxxxxxx{", false},
{"}xxxxxxxxxx", false},
{"x{}xxx{xx", false},
}
func TestCheck(t *testing.T) {
for _, tc := range tcs {
t.Run(tc.input, func(t *testing.T) {
if ok := check(tc.input); ok != tc.output {
t.Errorf("expected %v got, %v", tc.output, ok)
}
})
}
}
func BenchmarkCheck(b *testing.B) {
for _, tc := range tcs {
b.Run(tc.input, func(b *testing.B) {
for i := 0; i < b.N; i++ {
check(tc.input)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment