Weekly Puzzle: Don't Drink The Water

Last week: Dubstep

Last week the challenge was to decode a song that had been ruined remixed with dubstep wubwubwubs.

In addition to bragging rights the winners will also get fabulous Hack the Planet t-shirts.

The "Who's on first?" Award:

goes to Kevin Marker for being the first to complete the puzzle.

def song_decoder(song)  
  x = song.gsub("WUB", " ").split.join(" ")
end  

The "Universal Translator" Award:

goes to Abdullah Alger for completing the puzzle in python, ruby and javascript.

Python

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

Ruby

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

Javascript

function songDecoder(song){  
  return song.replace(/(WUB)+/g," ").trim()
}

The "Doesn't Mind a lot of Typing" Award:

goes to Chris Slowik for his solution written in Swift:

import Foundation

func songDecoder(remix: String) -> String {  
    return remix.stringByReplacingOccurrencesOfString("WUB", withString: " ").componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).filter({!$0.isEmpty}).joinWithSeparator(" ")
}

print(songDecoder("WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB"))  

I was shocked at how verbose string manipulation code is in Swift. It is an interesting and powerful language, but it's still missing a lot of the nicer ergonomic features of other languages.

It seems that one way people get around this is by extending the standard Swift String class to add replace() and trim() methods:

extension String {  
    func replaceMatches(regexp: String, with replacement: String) -> String {
        if let regex = try? NSRegularExpression(pattern: regexp, options: []) {
            return regex.stringByReplacingMatchesInString(
                self,
                options: [],
                range: NSMakeRange(0, self.characters.count),
                withTemplate: replacement
            )
        }

        return self
    }

    func trim() -> String
    {
        return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
    }
}

Once you have that you can rewrite the solution as:

func songDecoder(remix: String) -> String {  
    return remix.replaceMatches("(WUB)+", with: " ").trim();
}

This makes it resemble the other languages quite closely.

This Week: Don't Drink the Water

Given a two-dimensional array representation of a glass of mixed liquids, sort the array such that the liquids appear in the glass based on their density. (Lower density floats to the top) The width of the glass will not change from top to bottom.

======================
|   Density Chart    |
======================
| Honey   | H | 1.36 |
| Water   | W | 1.00 |
| Alcohol | A | 0.87 |
| Oil     | O | 0.80 |
----------------------
[                            [
 ['H', 'H', 'W', 'O'],        ['O','O','O','O']
 ['W', 'W', 'O', 'W'],  =>    ['W','W','W','W']
 ['H', 'H', 'O', 'O']         ['H','H','H','H']
 ]                           ]

The glass representation may be larger or smaller. If a liquid doesn't fill a row, it floats to the top and to the left.

Submission Instructions

Complete the puzzle on CodeWars and then follow these instructions to get the link to your solution and email that link to weeklypuzzle@bloc.io or tweet it at @trybloc. Submit by Friday February 5th 9am PST to be included.