Sunday, 6 December 2015

Kata Day 12: Dubstep


Problem Description: 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

songDecoder("WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB")
  // =>  WE ARE THE CHAMPIONS MY FRIEND

Level: 6kyu

Link to Kata: Dubstep

This is the first Kata where I am using Regex which is a powerful tool to work with strings and its implementation can be found in various languages including Javascript. Here I assigned the variable 'find' to a value of "WUB".  Then I assigned another variable 're' to locate te positions where "WUB" is present in the string. Then I replaced all the positions where "WUB" was present with a space(" ").

I implemented the following regex command to remove all the occurences of "WUB" with a space so that I do not end up giving more than 1 space to a continuous occurrence of "WUB".

str.replace(/\s+/g, ' ');

The '+' takes continuous occurrences into account and then replace them with a single space. We return the string by using a trim function on it so as to remove any space in the starting or ending of the string.

Here is a link to the code: Kata Day 12

Please mention your suggestions or your doubts in the comment below.

Happy coding. :)

No comments:

Post a Comment