寫 code 前,先讀懂題目是很重要的,
前面寫起來挺順的,實際丟 Codewars LV5 測試時,
代誌不是憨人想的那麼簡單,
例外訊息沒留意到,卡在後面搞懂問題及下關鍵字找答案,
為了不耽誤各位時間,
後半段解法明天繼續。
1
2
3
4
5Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.
Examples
pig_it('Pig latin is cool') # igPay atinlay siay oolcay
pig_it('Hello world !') # elloHay orldway !
1
2
3
4
5
6def pig_it text
# ...
end
Test.assert_equals(pig_it('Pig latin is cool'),'igPay atinlay siay oolcay')
Test.assert_equals(pig_it('This is my string'),'hisTay siay ymay tringsay');
影片解題:
答案:
1
2
3
4
5#Simple Pig Latin
def pig_it text
text.split.map{ |x| x.chars.rotate.insert(-1, "ay").join }.join' '
end
#非正確答案喔!!
下集待續..
本文同步發布於 小菜的 Blog https://riverye.com/