Spotify Python Puzzles

Not that long ago I discovered the Spotify puzzles and thought they would be a good excuse to improve my limited Python knowledge.

So here are my attempts at solving the ”Reverse” and “Zipfsong” puzzles in Python:

Reverse:

#!/usr/bin/env python
import sys

for number in sys.stdin:
    print int('{:b}'.format(int(number))[::-1], 2)

Zipfsong:

#!/usr/bin/env python
import sys
import heapq

num_songs, num_select = (int(n) for n in sys.stdin.readline().split())

songs = []
for line_num in xrange(1, num_songs + 1):
    plays, song = sys.stdin.readline().split()
    quality = int(plays) * line_num
    heapq.heappush(songs, (-quality, line_num, song))

for i in xrange(num_select):
    print heapq.heappop(songs)[2]

I’m quite new to Python so this might not be the best Python code in the world. If you can think of a way it could be improved though, I would love to know! Just leave a comment.

How to install Aptana Studio 3.3.2 in Ubuntu 12.10 (Quantal Quetzal)

Before anything else, install the prerequisites

sudo apt-get install openjdk-7-jre libwebkitgtk-1.0-0 git-core

Although Aptana Studio doesn’t officially support OpenJDK, I’ve yet to encounter any problem using it with Aptana Studio.

To use the Sun Java instead, do the following:

sudo apt-get install libwebkitgtk-1.0-0 git-core

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer
#source

Installing Aptana Studio

In order to make installation of Aptana Studio easier I’ve put i into a small shell script.

1. Download the script

wget https://www.samclarke.com/wp-content/uploads/2012/12/AptanaStudioInstall.sh

The source of the above shell script is available here or as a Gist here.

2. Give the script executable permissions

chmod +x AptanaStudioInstall.sh

3. Run it

sudo ./AptanaStudioInstall.sh

Or to do all of the above as a single command:

wget https://www.samclarke.com/wp-content/uploads/2012/12/AptanaStudioInstall.sh && chmod +x AptanaStudioInstall.sh && ./AptanaStudioInstall.sh

If the script isn’t run with root privileges (using sudo) it will ask you for your password so it can run with root privileges to install Aptana Studio.

How to install Cloud 9 IDE on Ubuntu 12.04 LTS (Precise Pangolin)

The easiest way to use Cloud 9 is to simply use it online (in the Cloud) at c9.io.

But if like me you want to run it on your own computer with Ubuntu 12.04, here’s how.

1. Install the prerequisites

sudo apt-get install -y build-essential g++ curl libssl-dev apache2-utils git libxml2-dev
# Remove any previous version of node
sudo apt-get remove nodejs

2. Install node using nvm
Nvm makes it easy to install and switch between multiple version of Node. Currently Cloud9 works with Node version 0.6.19, which unfortunately means you can’t use the version provided in the Ubuntu repositories.

To install nvm along with Node 0.6.19, simply do:

git clone git://github.com/creationix/nvm.git ~/nvm
echo '. ~/nvm/nvm.sh' >> ~/.bashrc && . ~/.bashrc
nvm install v0.6.19
nvm use v0.6.19

3. Install sourcemint

sudo npm install -g sm

4. Install Cloud 9

cd ~/
git clone https://github.com/ajaxorg/cloud9.git cloud9
cd cloud9
sudo sm install

Finished!
That’s it, you can now run it!

nvm use 0.6.19 && cd ~/cloud9 && bin/cloud9.sh

To start it in a directory do:

nvm use 0.6.19 && cd ~/cloud9 && bin/cloud9.sh -w ~/path/to/edit

Then go to http://localhost:3131

Add menu entry for XAMPP in Ubuntu 12.04

You will need to install python-glade2 for the XAMPP control panel to work:

sudo apt-get install python-glade2

Then create a file called XAMPP-Control-Panel.desktop in /usr/share/applications/ and put the following in it:

[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=XAMPP Control Panel
GenericName=Control Panel
Comment=XAMPP Control Panel
Exec=gksudo "/opt/lampp/share/xampp-control-panel/xampp-control-panel"
Icon=/opt/lampp/htdocs/favicon.ico
StartupNotify=true
StartupWMClass="xampp-control-panel.py"
Terminal=false
Type=Application
Categories=Developer;

Alternatively do the following:

wget http://www.samclarke.com/wp-content/uploads/2012/04/XAMPP-Control-Panel.desktop
sudo mv XAMPP-Control-Panel.desktop /usr/share/applications/XAMPP-Control-Panel.desktop

By default (in Ubuntu 12.04) the control panel will not be themed as it needs to run as root. To set the root users theme to your current theme, simply do:

sudo ln -s ~/.themes /root/.themes
sudo ln -s ~/.icons /root/.icons
sudo ln -s ~/.fonts /root/.fonts

How to install Aptana Studio 3 on Ubuntu 12.04 LTS (Precise Pangolin)

Update: An updated version of these instructions for Ubuntu 12.10 (Quantal Quetzal) is available here.

1. Install the prerequisites

sudo apt-get install openjdk-7-jdk libjpeg62 libwebkitgtk-1.0-0 git-core

Although Aptana Studio doesn’t officially support OpenJDK, I’ve not encountered any problems, however I’ve not done extensive testing. Alternatively, to use the Sun JDK, do the following:

sudo apt-get install libjpeg62 libwebkitgtk-1.0-0 git-core

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

#source

libjpeg62 is important, without it you will get errors like this:

An internal error has occurred.
No more handlesNative code library failed to load. (java.lang.UnsatisfiedLinkError: /opt/Aptana_Studio_3/plugins/com.aptana.swt.webkitbrowser.linux.x86_64_1.0.0.1295409059/os/linux/x86_64/libcefjni.so: libjpeg.so.62: cannot open shared object file: No such file or directory)

2. Download Aptana Studio

You can download Aptana Studio 3 here. Select the “Standalone Version” if not selected and click download.

3. Extract Aptana Studio

sudo unzip [name of Aptana Studio ZIP file here].zip -d /opt

4. Add the menu shortcut

wget http://www.samclarke.com/wp-content/uploads/2012/04/AptanaStudio3.desktop
sudo mv AptanaStudio3.desktop /usr/share/applications/AptanaStudio3.desktop

For those of you which are security conscious, you can check the contents of AptanaStudio3.desktop here or by entering the URL above.