鱼C论坛

 找回密码
 立即注册
查看: 1271|回复: 6

[已解决]求各位大佬改进一下我的代码,谢谢🙏

[复制链接]
发表于 2019-8-19 11:33:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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

  1. def song_decoder(song):
  2.     song_sub = 'WUB'
  3.     split_num = song.count(song_sub)  #被分割的次数
  4.     song_list = song.split(song_sub, split_num)
  5.     list_len = len(song_list)
  6.     song_list_ele = 0  #song_list列表里元素的index
  7.     while song_list_ele < list_len:
  8.         if song_list[song_list_ele] == '':
  9.             del song_list[song_list_ele]
  10.             list_len -= 1
  11.         else:
  12.             song_list_ele += 1
  13.     song_string = ' '.join(song_list)
  14.     return song_string.strip()
复制代码
最佳答案
2019-8-19 12:32:35
本帖最后由 Seawolf 于 2019-8-19 12:40 编辑
  1. import re
  2. def song_decoder(song):
  3.     return re.sub('(WUB)+', ' ', song).strip()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-19 12:31:43 | 显示全部楼层
def song_decoder(song):
    song_sub = 'WUB'
    song_list = song.split(song_sub)
    song_list = [i for i in song_list if(len(str(i)))!=0]
    song_string = ' '.join(song_list).title()
    return song_string
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-19 12:32:33 | 显示全部楼层
  1. def song_decoder(song):
  2.     song_sub = 'WUB'
  3.     song_list = song.split(song_sub)
  4.     song_list = [i for i in song_list if(len(str(i)))!=0]
  5.     song_string = ' '.join(song_list).title()
  6.     return song_string
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-8-19 12:32:35 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Seawolf 于 2019-8-19 12:40 编辑
  1. import re
  2. def song_decoder(song):
  3.     return re.sub('(WUB)+', ' ', song).strip()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-19 15:50:34 | 显示全部楼层
你前面那些英文是什么鬼?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-20 09:21:10 | 显示全部楼层
_2_ 发表于 2019-8-19 15:50
你前面那些英文是什么鬼?

就是需要写这段代码的背景
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-20 14:36:05 | 显示全部楼层
ladiesmansy 发表于 2019-8-20 09:21
就是需要写这段代码的背景

用划词即译整个界面都放不下了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 17:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表