DevBlog – JoseTomasTocino.com

Horizontal messaging bus using postal.js

I just came across this series of posts about JavaScript, and they’re great! They cover the most recent technologies in the front-end scene nowadays. Among them, there was a concept that I had never heard of before: mediated events. When you use (for instance) jQuery’s on, you’re saying that whenever an event is triggered by an element, a callback must be launched.

With mediated events, you don’t have that tight coupling. Instead, elements (and any other kind of event publisher) publishes events about a certain topic on a certain channel with some attached data. Then, some other objects subscribe callbacks to those channels and topics. You could have different channels and, within those, different topics.

In this example, I’m going to use postal.js, one of the most popular libraries for this kind of tasks. I’ve set up a MeteoGuy who publishes reports on the status of the wind and rain. Also, I’ve set up two listeners who are subscribed to wind and rain-related events respectivelly. As you can see, the listeners are not subscribed to TheMeteoGuy directly, but to TheWeatherChannel.

To better see the power of these kind of messages you could, for instance, add a NewspaperWeatherSection that would publish to the same TheWeatherChannel, and the listeners would receive the messages as well.

As you can see, the posibliities behind this are endless. However, here’s a word of caution. If you abuse this, you’ll end up with a lot of messages being sent from everwhere, and no easy way of tracking them.


Bookmarklet to open 500px images on a new tab

Whenever you try to right-click an image in 500px, you’ll get a warning message saying that the picture is copyrighted and stuff. You can’t therefore save the image because the context menu does not appear. However, there’s an easy fix for this. The image is contained in a div with the id thephoto, so you can easily get the images’s source path and open it in a new window or tab using the following javascript snippet:

To use it, just drag and drop this link: 500px Isolate Image to your bookmarks toolbar. Then, whenever you’re surfing 500px and want to download an image, click the bookmarklet and you’ll see a new window with the image for you to download.


FB Hacker Cup – Qualification Round

Last weekend, Facebook launched the qualification round of their Hacker cup, an annual worldwide programming competition. The way this competition works is pretty similar to that of other programming contests out there: a series of programming problems are presented, each of which has a sample input and a sample output along with a (sometimes minimal) explanation of the problem. Once you’ve programmed a solution using the sample input and output, you request the test input. That’s the real deal. Usually test inputs pose corner cases where you’re algorithm will likely have a bad time if it’s not properly thought out. The last step is to send the output for the test input along with the source code of your solution.

The qualification round presented three problems. Solving any of them qualifies you to compete in round 1. Here’s my take on each of them.

Beautiful strings

For the first problem (click here to see the full formulation) you had to calculate the grade of beautifulness of a set of strings. Each letter is assigned an unknown value (from 1 to 26), and the score for the string is the sum of the values of all the letters, ignoring punctuation and case. After looking at the sample cases I realized that the letters had their score assigned depending on the repetitions of each letter within the string, so the letter that appeared more often had a value of 26, the next more common had a value of 25 and so on.

Therefore, it was a matter of filtering the string, counting the appearances of each letter and calculating the total value. Here’s my take in Python:

After sending it I started to think about minifying my solution… and it came down to ONE SINGLE LINE, take a look:

Balanced smileys

The second problem was about balancing parentheses according to some rules about valid strings. Pretty easy stuff using recursion and regular expressions:

Find the min

The last problem was obviously the hardest. You’re better off reading the original formulation here. I didn’t give much thought to it and the implementation was sub-par, so I could not provide the output for the test input within the given time frame (6 minutes). The values for the k elements repeated after some period, which I could not find out. I’ve posted a question in math.stackexchange to see if they can shed some light on the subject.

Anyways, here’s my brute-force algorithm for the last question:

I’m looking forward to seeing the problems in the next round!