Created
July 16, 2020 07:03
-
-
Save justinbellamy/f7ba5a8d183d8fa13dbc89e200b40396 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 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