Skip to content

Commit

Permalink
Add solution for cw-indexed-capitaliztion
Browse files Browse the repository at this point in the history
  • Loading branch information
skosovsky committed Dec 14, 2023
1 parent 5205928 commit 731e3e1
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cw-indexed-capitaliztion/README.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions cw-indexed-capitaliztion/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/skosovsky/algo/cw-indexed-capitaliztion

go 1.21.5
34 changes: 34 additions & 0 deletions cw-indexed-capitaliztion/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
53 changes: 53 additions & 0 deletions cw-indexed-capitaliztion/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 731e3e1

Please sign in to comment.