mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 18:34:42 +01:00
20201126
This commit is contained in:
74
20201126/main.go
Normal file
74
20201126/main.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
|
||||
func findMaxSubLen(src string) int {
|
||||
l := 0
|
||||
r := 0
|
||||
max := 0
|
||||
sm := make(map[uint8]struct{})
|
||||
for {
|
||||
if l == len(src) {
|
||||
return max
|
||||
}
|
||||
for {
|
||||
if r == len(src) {
|
||||
return max
|
||||
}
|
||||
c := src[r]
|
||||
if _, found := sm[c]; found {
|
||||
sm = make(map[uint8]struct{})
|
||||
l++
|
||||
r = l
|
||||
break
|
||||
} else {
|
||||
sm[c] = struct{}{}
|
||||
if len(sm) > max {
|
||||
max = len(sm)
|
||||
}
|
||||
r++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func findMaxSubLen2(src string) int {
|
||||
l := 0
|
||||
r := 0
|
||||
max := 0
|
||||
var ss []uint8
|
||||
for {
|
||||
if l == len(src) {
|
||||
return max
|
||||
}
|
||||
for {
|
||||
if r == len(src) {
|
||||
return max
|
||||
}
|
||||
c := src[r]
|
||||
if i := indexOfValue(c, ss); i != -1 {
|
||||
ss = ss[i:]
|
||||
l += i + 1
|
||||
r++
|
||||
break
|
||||
} else {
|
||||
ss = append(ss, c)
|
||||
if len(ss) > max {
|
||||
max = len(ss)
|
||||
}
|
||||
r++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func indexOfValue(n uint8, src []uint8) int {
|
||||
for i := len(src) - 1; i >= 0; i-- {
|
||||
if src[i] == n {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
121
20201126/main_test.go
Normal file
121
20201126/main_test.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_findMaxSubLen(t *testing.T) {
|
||||
type args struct {
|
||||
src string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want int
|
||||
}{
|
||||
{
|
||||
"example_1",
|
||||
args{"abccefg"},
|
||||
4,
|
||||
},
|
||||
{
|
||||
"example_2",
|
||||
args{"bbbbb"},
|
||||
1,
|
||||
},
|
||||
{
|
||||
"example_3",
|
||||
args{"pwwkew"},
|
||||
3,
|
||||
},
|
||||
{
|
||||
"example_4",
|
||||
args{""},
|
||||
0,
|
||||
},
|
||||
{
|
||||
"example_5",
|
||||
args{"2"},
|
||||
1,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := findMaxSubLen(tt.args.src); got != tt.want {
|
||||
t.Errorf("findMaxSubLen() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_findMaxSubLen2(t *testing.T) {
|
||||
type args struct {
|
||||
src string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want int
|
||||
}{
|
||||
{
|
||||
"example_1",
|
||||
args{"abccefg"},
|
||||
4,
|
||||
},
|
||||
{
|
||||
"example_2",
|
||||
args{"bbbbb"},
|
||||
1,
|
||||
},
|
||||
{
|
||||
"example_3",
|
||||
args{"pwwkew"},
|
||||
3,
|
||||
},
|
||||
{
|
||||
"example_4",
|
||||
args{""},
|
||||
0,
|
||||
},
|
||||
{
|
||||
"example_5",
|
||||
args{"2"},
|
||||
1,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := findMaxSubLen2(tt.args.src); got != tt.want {
|
||||
t.Errorf("findMaxSubLen2() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_hasValue(t *testing.T) {
|
||||
type args struct {
|
||||
n uint8
|
||||
src []uint8
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want int
|
||||
}{
|
||||
{
|
||||
"test1",
|
||||
args{1, []uint8{1, 2}},
|
||||
0,
|
||||
},
|
||||
{
|
||||
"test2",
|
||||
args{1, []uint8{3, 2}},
|
||||
-1,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := indexOfValue(tt.args.n, tt.args.src); got != tt.want {
|
||||
t.Errorf("indexOfValue() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user