見習村08 - Mexican Wave

08 - Mexican Wave

Don’t say so much, just coding…

Instruction

In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up.

Rules:

  1. The input string will always be lower case but maybe empty.
  2. If the character in the string is whitespace then pass over it as if it was an empty seat

Examples:

1
wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]

Ruby

Init

1
2
3
def wave(str)
# ...
end

Sample Testing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
result = ["Hello", "hEllo", "heLlo", "helLo", "hellO"];
Test.assertDeepEquals(wave("hello"),result, "Should return: '"+result+"'");

result = ["Codewars", "cOdewars", "coDewars", "codEwars", "codeWars", "codewArs", "codewaRs", "codewarS"];
Test.assertDeepEquals(wave("codewars"), result, "Should return: '"+result+"'");

result = [];
Test.assertDeepEquals(wave(""), result, "Should return: '"+result+"'");

result = ["Two words", "tWo words", "twO words", "two Words", "two wOrds", "two woRds", "two worDs", "two wordS"];
Test.assertDeepEquals(wave("two words"), result, "Should return: '"+result+"'");

result = [" Gap ", " gAp ", " gaP "];
Test.assertDeepEquals(wave(" gap "), result, "Should return: '"+result+"'");

Javascript

Init

1
2
3
function wave(str){
// Code here
}

Sample Testing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
result = ["Hello", "hEllo", "heLlo", "helLo", "hellO"];
Test.assertDeepEquals(wave("hello"),result, "Should return: '"+result+"'");

result = ["Codewars", "cOdewars", "coDewars", "codEwars", "codeWars", "codewArs", "codewaRs", "codewarS"];
Test.assertDeepEquals(wave("codewars"), result, "Should return: '"+result+"'");

result = [];
Test.assertDeepEquals(wave(""), result, "Should return: '"+result+"'");

result = ["Two words", "tWo words", "twO words", "two Words", "two wOrds", "two woRds", "two worDs", "two wordS"];
Test.assertDeepEquals(wave("two words"), result, "Should return: '"+result+"'");

result = [" Gap ", " gAp ", " gaP "];
Test.assertDeepEquals(wave(" gap "), result, "Should return: '"+result+"'");

Thinking

想法(1): 將所有字都先轉為小寫,在透過跑迴圈來看第幾個字為大寫 + 其餘小寫
想法(2): 複製原本傳入的值出來後,再將大寫的字 assign 回複製出來的值

https://ithelp.ithome.com.tw/upload/images/20200923/20120826UtmZFyXlPA.jpg
圖片來源:Unsplash C D-X

Hint & Reference

Solution

Ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Solution 1
def wave(str)
result = []
chars = str.downcase.chars
chars.each_with_index do |char, index|
next if char == " "
result << str[0...index] + char.upcase + str[index+1..-1]
end
result
end

# Solution 2
def wave(str)
result = []
str.size.times do |index|
next if str[index] == ' '
result << str.dup
result[-1][index] = result[-1][index].upcase
end
result
end

Javascript

1
2
3
4
5
6
7
8
9
// Solution 1
function wave(str){
var result = [];
for (i = 0; i < str.length; i++) {
if (str[i] != ' ')
result.push(str.slice(0, i) + str[i].toUpperCase() + str.slice(i + 1))
}
return result;
}