-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution for cw-indexed-capitaliztion
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |