Created
October 2, 2014 22:38
-
-
Save jgrar/7c8b206f29fc0db774be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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