字串的动次踏次,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 纾压一下了,对音乐兴趣的朋友也可以尝试看看这类曲风喔,或许会意外地打开新世界的大门~


<<:  Day-04 说明什麽是Rack?

>>:  【Day19】用Bootstrap和Fontawesome来刻我们的计数器吧 (´∀`)!!

环境建置

开发环境: Visual Studio Community 2019 程序语言: C# 云端服务: ...

Day1 研究AR的起因&初心(刚出新手村的萌新)

选择研究/探讨AR相关技术是因为希望未来可以做出一些很酷的东西,像Pokemon GO或是游戏王卡的...

Day30 - Android APP 最後一天

【感谢】 感谢跟完这三十天的所有人 感谢团队的各位夥伴一起鞭策前进 感谢自己没有放弃 【心得】 这是...

Day 23 - Promise

Promise 解决了回调地狱,在处理一些需要花费比较长时间的任务时使用 Promise 就可以进行...