diff --git a/cw-indexed-capitaliztion/README.md b/cw-indexed-capitaliztion/README.md new file mode 100644 index 0000000..edc9234 --- /dev/null +++ b/cw-indexed-capitaliztion/README.md @@ -0,0 +1,10 @@ +# Indexed capitalization + +Given a string and an array of integers representing indices, capitalize all letters at the given indices. + +For example: + +'capitalize("abcdef",[1,2,5]) = "aBCdeF"' +'capitalize("abcdef",[1,2,5,100]) = "aBCdeF"'. There is no index 100. + +The input will be a lowercase string with no spaces and an array of digits. diff --git a/cw-indexed-capitaliztion/go.mod b/cw-indexed-capitaliztion/go.mod new file mode 100644 index 0000000..4677132 --- /dev/null +++ b/cw-indexed-capitaliztion/go.mod @@ -0,0 +1,3 @@ +module github.com/skosovsky/algo/cw-indexed-capitaliztion + +go 1.21.5 diff --git a/cw-indexed-capitaliztion/main.go b/cw-indexed-capitaliztion/main.go new file mode 100644 index 0000000..6ddc8ee --- /dev/null +++ b/cw-indexed-capitaliztion/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "unicode" +) + +func main() { + fmt.Println(Capitalize("abcdef", []int{1, 2, 5})) //nolint:gomnd +} + +func Capitalize(st string, arr []int) string { + var line = []rune(st) + var newline []rune + digits := map[int]bool{} + + for _, v := range arr { + if v > len(line) { + continue + } + digits[v] = true + } + + for i, _ := range line { + symbol := line[i] + if digits[i] { + newline = append(newline, unicode.ToUpper(symbol)) + } else { + newline = append(newline, symbol) + } + } + + return string(newline) +} diff --git a/cw-indexed-capitaliztion/main_test.go b/cw-indexed-capitaliztion/main_test.go new file mode 100644 index 0000000..8899a1c --- /dev/null +++ b/cw-indexed-capitaliztion/main_test.go @@ -0,0 +1,53 @@ +package main + +import "testing" + +func TestCapitalize(t *testing.T) { + type args struct { + st string + arr []int + } + tests := []struct { + name string + args args + want string + }{ + { + name: "Example 1", + args: args{"abcdef", []int{1, 2, 5}}, + want: "aBCdeF", + }, + { + name: "Example 2", + args: args{"abcdef", []int{1, 2, 5, 100}}, + want: "aBCdeF", + }, + { + name: "Example 3", + args: args{"codewars", []int{1, 3, 5, 50}}, + want: "cOdEwArs", + }, + { + name: "Example 4", + args: args{"abRacaDabRA", []int{2, 6, 9, 10}}, + want: "abRacaDabRA", + }, + { + name: "Example 5", + args: args{"codewarriors", []int{5}}, + want: "codewArriors", + }, + { + name: "Example 6", + args: args{"indexinglessons", []int{0}}, + want: "Indexinglessons", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Capitalize(tt.args.st, tt.args.arr); got != tt.want { + t.Errorf("Capitalize() = %v, want %v", got, tt.want) + } + }) + } +}