CodeWars-Dubstep

skrillex

今天来聊聊电音(●'◡'●),你最喜欢的DJ是哪位呢?
Breakn' A Sweat -Skrillex / The Doors

一.题目

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. 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

一些英语单词:
1.dubstep music
电音分支之一

2.he isn't into modern music
他不喜欢现代音乐,
这是比较口语化的表达。

3.initial song 、original song
inital / original adj. 初始的、原始的
这里都是指‘原曲’,被混音前的曲子。

翻译起来的话:

Polycarpus在一家夜店担任DJ,并且他经常在他的表演中使用dubstep music。最近,他已经使用几首旧曲子编成一首新的混音。

假定,这首混音由一系列的单词组成。为了制作这首混音,Polycarpus插入了一定数量的单词‘WUB’,可以在第一个单词之前插入‘WUB’或者不插入,可以在最后一个单词之后插入也可以不插入,但是在单词与单词之间,至少要插入一个‘WUB’,然后这个男孩就将这些所有的单词包括‘WUB’整合在一起(没有空格间隔),形成一个字符串然后播放这首曲子。

举个栗子,‘I AM X’这句话可以混音变成‘WUBWUBIWUBAMWUBWUBX’,但是不可以变成‘WUBWUBIAMWUBX’(单词与单词之间至少要有一个‘WUB’,I和AM中间没有所以不能这样混音)

最近,Jonny听过了Polycarpus新的混音,但因为他对现代音乐不感兴趣,所以他决定找出原曲是什么。请帮帮他。

输入
输入一个非空的字符串,只有大写字母组成,这个字符串不会超过200个字符。

输出
返回原曲的单词,用空格间隔。

初始代码

song_decoder

举个栗子

Test.assert_equals(song_decoder("AWUBBWUBC"), "A B C","WUB should be replaced by 1 space")
Test.assert_equals(song_decoder("AWUBWUBWUBBWUBWUBWUBC"), "A B C","multiples WUB should be replaced by only 1 space")
Test.assert_equals(song_decoder("WUBAWUBBWUBCWUB"), "A B C","heading or trailing spaces should be removed")

二.思路和代码

思路

先来看看函数的大概的框架

输入 : 一个字符串
内部操作:对字符串进行操作,具体是删除和整合。
输入 : 一个字符串

接着我们来看看具体怎么操作,
读完题目之后,想到一个概念——正则表达式。
正则表达式又称为规则表达式(regular expression)
我对正则表达式的理解是:匹配合乎规则的字符或字符串

以本题为例,要将‘WUB’去掉,用空格代替。
但是WUB有可能连续出现,如果使用字符串的replace()函数,以"AWUBWUBWUBBWUBWUBWUBC"为例,就会变成字符串‘A···B···C’(每个·代表一个空格),A与B,B与C之间间隔3个空格,而题目要我们只能间隔一个空格。(PS:在这里当然也有别的处理方法,不过我们先讲正则)。
如果我们要设定规则的话,那规则就是WUB至少出现一次。

正则的写法就是(WUB)+。

代码

import re
def song_decoder(song):
    song = re.sub(r'(WUB)+'," ",song)
    song = song.strip()
    return song

代码解析

导入模块 re (正则表达式)
利用模块内的sub()函数
第一个参数是要匹配的正则表达式,第二个参数是要被替换成什么,第三个参数就是要进行匹配的字符串。
这里关键是要理解正则表达式的意思,(WUB)+代表WUB出现至少一次或多次的字符串,具体实例是
WUB
WUBWUB
WUBWUBWUB
WUBWUBWUBWUB等等
将这些字符串替换成空格(一个空格)。
然后song.strip()去掉两端空格就好了

三.最优代码解

Best Practices.png

排名第一个答案用到了一个转换的想法。字符串列表互相转换

song原本是字符串

song.replace('WUB',' ')
replace()将字符串内WUB的字符替换为空格

song.replace('WUB',' ').split()
replace()将字符串内WUB的字符替换为空格,然后.split() 用空格将字符串分割变成列表(对的没有错,字符串使用split()方法后,就变为了列表)

" ".join(song.replace('WUB', ' ').split())
replace()将字符串内WUB的字符替换为空格,然后.split() 用空格将字符串分割变成列表,然后" ".join再将这个列表变为字符串,原本列表之间用空格连接。

字符串列表的相互转换,使用的就是两个方法

还是以"AWUBWUBWUBBWUBWUBWUBC"为例,replace()方法之后,字符串变成‘A···B···C’(每个·代表一个空格)
接着
str.split() ——> list (字符串变成了列表)
['A','B','C']
' '.join(list) ——> str (列表变成了字符串)
'A·B·C'

spilt()函数:里面可以填参数,默认是字符串以空格作为间隔符,去掉间隔符,将字符串分割成不同元素。
‘ ’.join(list)函数:将列表里面的元素连接起来,冒号内填入作为连接的字符。本题用的是空格作为连接的字符。

四.总结

1.正则表达式
本题想考察的应该是正则表达式,找出字符串中要替换字符的规律,用正则表达式进行操作。关键是要字符的规律是什么?正则表达式可以应用在非常多的场合,判断是不是中国的手机号,判断是不是邮箱等等。用数字、字母和符号就能向计算机将规律表达出来。

2.字符串和列表的相互转换
str ————> list 用split()
list ————> str 用 ‘ ’.join
多多应用,熟练对象的互相转换。

你可能感兴趣的:(CodeWars-Dubstep)