iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 25
0
自我挑戰組

自我挑戰 Ruby 刷題 30 天系列 第 25

Day25 - Codewars 刷題

寫 code 前,先讀懂題目是很重要的,
前面寫起來挺順的,實際丟 Codewars LV5 測試時,
代誌不是憨人想的那麼簡單,
例外訊息沒留意到,卡在後面搞懂問題及下關鍵字找答案,
為了不耽誤各位時間,
後半段解法明天繼續。


題目(Simple Pig Latin)

Move 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 !
def 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');

影片解題:
Yes


答案:

#Simple Pig Latin
def pig_it text
  text.split.map{ |x| x.chars.rotate.insert(-1, "ay").join }.join' '
end
#非正確答案喔!!

下集待續..

本文同步發布於 小菜的 Blog https://riverye.com/


上一篇
Day24 - Codewars 刷題
下一篇
Day26 - Codewars 刷題
系列文
自我挑戰 Ruby 刷題 30 天31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言