mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 20:04:43 +01:00
20201224
This commit is contained in:
37
20201224/main.go
Normal file
37
20201224/main.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func isEcho(s string) bool {
|
||||||
|
var sc []uint8
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
c := s[i]
|
||||||
|
if isAlphabetAndDigit(c) {
|
||||||
|
if c <= 'Z' && c >= 'A' {
|
||||||
|
c += 'a' - 'A'
|
||||||
|
}
|
||||||
|
sc = append(sc, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(sc) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
l, r := 0, len(sc)-1
|
||||||
|
for {
|
||||||
|
if l >= r {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if sc[l] != sc[r] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
l++
|
||||||
|
r--
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func isAlphabetAndDigit(c uint8) bool {
|
||||||
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
|
||||||
|
}
|
||||||
71
20201224/main_test.go
Normal file
71
20201224/main_test.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_isAlphabetAndDigit(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
c uint8
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"test1",
|
||||||
|
args{c: 'a'},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test2",
|
||||||
|
args{c: '1'},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test3",
|
||||||
|
args{c: '-'},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test4",
|
||||||
|
args{c: ','},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := isAlphabetAndDigit(tt.args.c); got != tt.want {
|
||||||
|
t.Errorf("isAlphabet() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_isEcho(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"test1",
|
||||||
|
args{s: "A man, a plan, a canal: Panama"},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test2",
|
||||||
|
args{s: "race a car"},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := isEcho(tt.args.s); got != tt.want {
|
||||||
|
t.Errorf("isEcho() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user