알고리즘/Codewars

[Codewars] Mumbling

2024. 6. 4. 17:45

Q. This time no story, no theory. The examples below show you how to write function accum

 

Examples:

accum("abcd") -> "A-Bb-Ccc-Dddd"

accum("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"

accum("cwAt") -> "C-Ww-Aaa-Tttt"

 

The parameter of accum is a string which includes only letters from a..z and A..Z.

 

S. for문 쓰는게 어색하다,,, list에 요소 추가할때는 add 쓰기!

String accum(String str) {
  List<String> result = [];
  for (var i = 0; i < str.length; i++) {
    result.add(str[i].toUpperCase() + str[i].toLowerCase() * i);
  }
  return result.join('-');
}