mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 19:54:43 +01:00
20201124
This commit is contained in:
27
20201124/main.go
Normal file
27
20201124/main.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func compress(str string) string {
|
||||||
|
var cl int32
|
||||||
|
count := 0
|
||||||
|
var ret string
|
||||||
|
for _, s := range str {
|
||||||
|
if s != cl {
|
||||||
|
cl = s
|
||||||
|
count = 1
|
||||||
|
ret += string(s) + "1"
|
||||||
|
} else {
|
||||||
|
count++
|
||||||
|
ret = ret[:len(ret)-1] + strconv.Itoa(count)
|
||||||
|
}
|
||||||
|
if len(ret) >= len(str) {
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
32
20201124/main_test.go
Normal file
32
20201124/main_test.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_compress(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"test1",
|
||||||
|
args{"aabcccccaaa"},
|
||||||
|
"a2b1c5a3",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test2",
|
||||||
|
args{"abbccd"},
|
||||||
|
"abbccd",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := compress(tt.args.s); got != tt.want {
|
||||||
|
t.Errorf("compress() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user