iT邦幫忙

2021 iThome 鐵人賽

DAY 4
0
自我挑戰組

從真「新」鎮出發!30天的刷題修行篇,讓寫程式成為新必殺技系列 第 4

字串的動次踏次,Ruby 30 天刷題修行篇第四話

大家好,這裡是 A Fei,又到了我們愉快的刷題練功時間,前三天的題型和 Number、Array 有關,今天我們來做另一個熱門的題型,就是字串 String。

有在寫程式的朋友一定不陌生,要怎麼對一段輸入的 String 進行加工和驗證,或是在大量資訊中,檢索我們要找的關鍵字,都考驗著一個網頁工程師對 String 方法的掌握程度。因此,多多練習和 String 有關的題型,能幫助我們保持手感。好了,讓我們直接進入今天的題目:

以下題目來源是 Codewars


Polycarpus works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them.

Let's assume that a song consists of some number of words (that don't contain WUB). To make the dubstep remix of this song, Polycarpus inserts a certain number of words "WUB" before the first word of the song (the number may be zero), after the last word (the number may be zero), and between words (at least one between any pair of neighbouring words), and then the boy glues together all the words, including "WUB", in one string and plays the song at the club.

For example, a song with words "I AM X" can transform into a dubstep remix as "WUBWUBIWUBAMWUBWUBX" and cannot transform into "WUBWUBIAMWUBX".

Recently, Jonny has heard Polycarpus's new dubstep track, but since he isn't into modern music, he decided to find out what was the initial song that Polycarpus remixed. Help Jonny restore the original song.

Input
The input consists of a single non-empty string, consisting only of uppercase English letters, the string's length doesn't exceed 200 characters

Output
Return the words of the initial song that Polycarpus used to make a dubsteb remix. Separate the words with a space.

Examples

song_decoder("WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB")
  #  =>  WE ARE THE CHAMPIONS MY FRIEND

題目難度:6 kyu
是否有在時限內解答:是

題目設定了一個情境,一名叫 Polycarpus 的夜店 DJ 表演 dubstep 音樂(什麼是 dubstep,這裡可以推薦給大家我非常喜歡的美國喜劇藝人搭檔 Key & Peele ,他們拍攝的情境喜劇短片 Dubstep,看完你就懂了),拉回主題,是說這個 Polycarpus 在 remix 經典老歌時,會將一段歌詞的前、後和單字之間插入 "WUB" (數量不固定)。但是有個叫 Jonny 的人不太喜歡 dubstep 曲風,便設法要還原這些 remix 過的歌曲。看到這裡,也大概搞懂題目要考什麼了,就是要我們消除一段 string 中的 "WUB",並且還原成正確歌詞。

解題重點:

  1. 把 "WUB" 替換成空格,也就是 " "。
  2. 要注意回傳的 string 中,開頭和結尾不可以有 " ",單字和單字間也只會有一個 " "。

第一點,我使用了 split 方法,把 string 按照 'WUB' 的位置打散,並重新組成一個新陣列(是的,split 這個方法的回傳值是陣列喔),以題目範例來說就是:

"WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB".split("WUB")
    # => ["", "WE", "ARE", "", "THE", "CHAMPIONS", "MY", "FRIEND"]

接著在用 select 過濾出所有單字,並且用 join 方法重新將陣列組合成 string (join 可以帶入分隔每個元素的值),以下就是我的解答:

def song_decoder(song)
  song.split('WUB').select {|i| i != ""}.join(' ')
end

對比評分最高的答案:

def song_decoder(song)
  song.gsub(/(WUB)+/, ' ').strip
end

查了一下資料(Ruby 官方手冊),gsub 方法帶入的第一個引數叫做 pattern,pattern 通常是一個正則表達式(Regexp),第二個引數是要替換的值。song.gsub(/(WUB)+/, ' ')就是將 song 中 "WUB" 檢索出來,並且以 ' '代替,而 strip 方法則是可以消除 string 的頭尾空格,也是非常便利的方法。

好啦,今天的文章就到這邊,我也要去聽 dubstep 紓壓一下了,對音樂興趣的朋友也可以嘗試看看這類曲風喔,或許會意外地打開新世界的大門~


上一篇
活到老,學到老,Ruby 30 天刷題修行篇第三話
下一篇
強敵!費波那契數的哥哥登場,Ruby 30 天刷題修行篇第五話
系列文
從真「新」鎮出發!30天的刷題修行篇,讓寫程式成為新必殺技16
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言