Spotify puzzles: round one

Some time ago I came across Spotify puzzles, a website in which Spotify’s engineers list a series of CS problems and gather solutions from interested people.

The interesting idea is that all solutions should be sent via mail, and an honorable (automated) judge tests the solution and sends the feedback.

It would be fun to test how can you break the judge, but that will be another post. Let’s start with solving the first problem, in order of difficulty:

Your task will be to write a program for reversing numbers in binary. For instance, the binary representation of 13 is 1101, and reversing it gives 1011, which corresponds to number 11.

Easy peasy huh?

First step: choosing a programming language to solve this problem: Ruby (“Your source code can be in C, C++, Java or Python (version 2.6)”). Python. First of all, I’ll take this opportunity to download and setup PyCharm, as the Python community seems to agree about it as the most versatile Python IDE.

Second task: coding! Even if the request is to make it compatible to Python2.6, I’ll try to make my code Python3-compatible (always looking forward!). PyCharm is based on Java (note: on OSX Yosemite you must install a JVM, Apple is not shipping a JVM by default as in the past) and it’s heavily inspired by IntelliJ IDEA and Eclipse (IMHO). PyCharm guarantees a pleasant experience, trying to suggest me some optimizations while I write the code; refactoring-aside, PyCharm has a complete set of solutions to solve my refactoring problems.

About the code: the process of converting between binary and decimal is straightforward, and we must be careful about the corner case checking (and throwing an exception). pycharm

Final step: testing! I always have been using Nose for my Python tests needing, but I wanted to get a glimpse of py.test. py.test is easy, streamlined and fast. It supports assert, among the others, for exception, and the syntax is clear and direct.py.test

On top of all, git manages my changes. All the code is available on my GitHub profile (spotify_puzzles repo). Stay tuned for the next puzzles!

One thought on “Spotify puzzles: round one”

Leave a Reply