Last active
June 1, 2016 14:50
-
-
Save dovys/c7ab6cb60b35572be20c3eec4fdc7683 to your computer and use it in GitHub Desktop.
palindrome.go
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
BenchmarkIsPalindrome5Chars-8 100000000 19.8 ns/op | |
BenchmarkIsPalindrome10Chars-8 50000000 27.2 ns/op | |
BenchmarkIsPalindrome20Chars-8 30000000 48.1 ns/op | |
BenchmarkIsPalindrome40Chars-8 20000000 112 ns/op | |
BenchmarkIsPalindrome80Chars-8 5000000 337 ns/op | |
BenchmarkIsPalindromeFalsy-8 100000000 13.8 ns/op | |
BenchmarkIsPalindromeFalsyVeryLong-8 2000000 709 ns/op |
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 tests | |
func IsPalindrome(text string) bool { | |
charLen := len(text) | |
bytes := []byte(text) | |
var byteLeft, byteRight byte | |
for i, j := 0, charLen-1; i < charLen; i++ { | |
byteLeft = bytes[i] | |
if byteLeft < 'A' || byteLeft > 'z' || (byteLeft > 'Z' && byteLeft < 'a') { | |
continue | |
} | |
for { | |
byteRight = bytes[j] | |
j-- | |
if byteRight < 'A' || byteRight > 'z' || (byteRight > 'Z' && byteRight < 'a') { | |
continue | |
} | |
if byteLeft != byteRight && byteLeft-32 != byteRight && byteLeft+32 != byteRight { | |
return false | |
} | |
break; | |
} | |
if i >= j { | |
break | |
} | |
} | |
return true | |
} |
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 tests | |
import ( | |
"testing" | |
"strings" | |
) | |
func TestIsPalindrome(t *testing.T) { | |
var inputs map[string]bool = map[string]bool{ | |
"No 'X' in Nixon": true, | |
"Step on no Pets": true, | |
"Taco cat": true, | |
"TacO cat Taco CAt": true, | |
"Stack cats": true, | |
"Amor, Roma": true, | |
"NommmoN": true, | |
"Mom Mom Mom Mom Mom Mom\" Mom Mom` Mom _Mom Mom -MomMom": true, | |
"Noooooooom ooooooooN": true, | |
"A": true, | |
"Aa": true, | |
"NommnoN": false, | |
"No X on Nixon": false, | |
"Nixon Nixon": false, | |
"Step on Pets": false, | |
} | |
for input, expectedOutput := range inputs { | |
if IsPalindrome(input) != expectedOutput { | |
t.Errorf("IsPalindrome(%s) was expected to be %#v.", input, expectedOutput) | |
} | |
} | |
} | |
func BenchmarkIsPalindrome5Chars(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
IsPalindrome("ooooo") | |
} | |
} | |
func BenchmarkIsPalindrome10Chars(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
IsPalindrome("Ta co ca T") | |
} | |
} | |
func BenchmarkIsPalindrome20Chars(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
IsPalindrome("Noooooooom ooooooooN") | |
} | |
} | |
func BenchmarkIsPalindrome40Chars(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
IsPalindrome("Mo m Mom Mom Mom Mom Mom Mom Mom Mom Mom") | |
} | |
} | |
func BenchmarkIsPalindrome80Chars(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
IsPalindrome(strings.Repeat("A", 80)) | |
} | |
} | |
func BenchmarkIsPalindromeFalsy(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
IsPalindrome("Not a palindrome") | |
} | |
} | |
func BenchmarkIsPalindromeFalsyVeryLong(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
IsPalindrome(strings.Repeat("N", 500) + "o") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment