From c5deee203f4bfe0785ec65ec5e2074d16ad2ad2f Mon Sep 17 00:00:00 2001 From: VicRen Date: Thu, 10 Dec 2020 09:04:34 +0800 Subject: [PATCH] 20201210 --- 20201210/main.go | 29 +++++++++++++++++++++++++++++ 20201210/main_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 20201210/main.go create mode 100644 20201210/main_test.go diff --git a/20201210/main.go b/20201210/main.go new file mode 100644 index 0000000..bf0aaed --- /dev/null +++ b/20201210/main.go @@ -0,0 +1,29 @@ +package main + +func main() { + +} + +func replace(src string) string { + var ret []rune + l := len(src) + cursor := 0 + for { + if cursor == l { + break + } + if cursor < l-1 && src[cursor] == '(' && src[cursor+1] == ')' { + ret = append(ret, 'o') + cursor += 2 + continue + } + if cursor < l-3 && src[cursor] == '(' && src[cursor+3] == ')' { + ret = append(ret, []rune{'a', 'l'}...) + cursor += 4 + continue + } + ret = append(ret, rune(src[cursor])) + cursor++ + } + return string(ret) +} diff --git a/20201210/main_test.go b/20201210/main_test.go new file mode 100644 index 0000000..2698bba --- /dev/null +++ b/20201210/main_test.go @@ -0,0 +1,43 @@ +package main + +import "testing" + +func Test_replace(t *testing.T) { + type args struct { + src string + } + tests := []struct { + name string + args args + want string + }{ + { + "test1", + args{ + "G()(al)", + }, + "Goal", + }, + { + "test2", + args{ + "G()()()()(al)", + }, + "Gooooal", + }, + { + "test3", + args{ + "(al)G(al)()()G", + }, + "alGalooG", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := replace(tt.args.src); got != tt.want { + t.Errorf("replace() = %v, want %v", got, tt.want) + } + }) + } +}