Skip to content

Instantly share code, notes, and snippets.

@jgrar
Created October 2, 2014 22:38
Show Gist options
  • Save jgrar/7c8b206f29fc0db774be to your computer and use it in GitHub Desktop.
Save jgrar/7c8b206f29fc0db774be to your computer and use it in GitHub Desktop.
package sumsuffix
func SumSuffix (s string) int {
var r int
for i := 0; i < len(s); i++ {
suffix := s[i:]
for j := 0; j < len(suffix); j++ {
if s[j] != suffix[j] {
break
}
r++
}
}
return r
}
package sumsuffix
import "testing"
var sumtests = []struct{
input string
expected int
}{
{"ababaa", 11},
{"aa", 3},
}
func TestSumSuffix (t *testing.T) {
for _, test := range sumtests {
if output := SumSuffix(test.input); output != test.expected {
t.Fatalf("SumSuffix(%s) = %d, want %d",
test.input, output, test.expected)
}
}
}
func BenchmarkSumSuffix (b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sumtests {
SumSuffix(test.input)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment