Skip to content

Instantly share code, notes, and snippets.

@gedex
Created August 31, 2014 19:06
Show Gist options
  • Save gedex/ab8fb8c432fb7a136825 to your computer and use it in GitHub Desktop.
Save gedex/ab8fb8c432fb7a136825 to your computer and use it in GitHub Desktop.
Palindrome in Go

From Wikipedia:

A palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward or reversed, with general allowances for adjustments to punctuation and word dividers. Famous examples include "Amor, Roma", "A man, a plan, a canal: Panama" and "No 'x' in 'Nixon'".

Demo

http://play.golang.org/p/oIzyUsGk58

package main
import "fmt"
func main() {
test := []string{
"a",
"abcdefgfedcba",
"abcdeffedcba",
"abcba",
"abcedfgfedcb",
}
for _, v := range test {
fmt.Printf("%20s: %v\n", v, isPalindrome(v))
}
}
func isPalindrome(s string) bool {
mid := len(s) / 2
last := len(s) - 1
for i := 0; i < mid; i++ {
if s[i] != s[last-i] {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment