Skip to content

Instantly share code, notes, and snippets.

@justinbellamy
Created July 16, 2020 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinbellamy/f7ba5a8d183d8fa13dbc89e200b40396 to your computer and use it in GitHub Desktop.
Save justinbellamy/f7ba5a8d183d8fa13dbc89e200b40396 to your computer and use it in GitHub Desktop.
package main
import(
"fmt"
"strings"
"regexp"
)
// IsPalendrome checks for strings and numbers
func IsPalendrome(value string) bool {
value = sanitize(value)
for i := 0; i < len(value)/2; i++ {
if value[i] != value[len(value)-i-1] {
return false
}
}
return true
}
func sanitize(value string) string {
reg, _ := regexp.Compile("[^A-Za-z0-9]+")
safe := reg.ReplaceAllString(value, "")
return strings.ToLower(strings.Trim(safe, ""))
}
func main() {
yesNumeric := IsPalendrome("2002")
noNumeric := IsPalendrome("2001")
yesString := IsPalendrome("Anna")
noString := IsPalendrome("Anya")
fmt.Println("IsPalendrome() yesNumeric:", yesNumeric, " noNumeric:", noNumeric, " yesString:", yesString, " noString:", noString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment