feat: palindrome
This commit is contained in:
30
pkg/valid_palindrome/main.go
Normal file
30
pkg/valid_palindrome/main.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package valid_palindrome
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var notAlphanumeric = regexp.MustCompile(`[^a-zA-Z0-9]`)
|
||||||
|
|
||||||
|
func KeepAlphanumeric(text string) string {
|
||||||
|
return notAlphanumeric.ReplaceAllString(text, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsPalindrome(text string) bool {
|
||||||
|
scrubbed := strings.ToLower(KeepAlphanumeric(text))
|
||||||
|
|
||||||
|
start := 0
|
||||||
|
end := len(scrubbed) - 1
|
||||||
|
|
||||||
|
for start < end {
|
||||||
|
if scrubbed[start] != scrubbed[end] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
start++
|
||||||
|
end--
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
14
pkg/valid_palindrome/main_test.go
Normal file
14
pkg/valid_palindrome/main_test.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package valid_palindrome_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.maximhutz.com/practice/pkg/valid_palindrome"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTwoSum(t *testing.T) {
|
||||||
|
assert.Equal(t, true, valid_palindrome.IsPalindrome("A man, a plan, a canal: Panama"))
|
||||||
|
assert.Equal(t, false, valid_palindrome.IsPalindrome("race a car"))
|
||||||
|
assert.Equal(t, true, valid_palindrome.IsPalindrome(" "))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user