Tuesday, May 31, 2011

Follow links to land in Philosophy

A couple of days back, the a cartoon was posted on xkcd titled "Extended Mind". In the mouse-over text, Randall Munroe wrote the following:
Wikipedia trivia: if you take any article, click on the first link in the article text not in parentheses or italics, and then repeat, you will eventually end up at "Philosophy".
I decided to test this theory and indeed managed to land to Philosophy every time !! Here is a demo run for the same:

Snickers > Candy Bar > Cocoa Solids > Chocolate > Mexico > Federation > Sovereign State > State (policy) > Social Sciences > List of Academic Disciplines > Academia > Community > Interaction > Causality > Event > Philosophy

I would be very interested in knowing if somebody find some topic that does not eventually lead back to Philosophy.

PS: To verify this theory I wrote a small Python program which starts from a random page and then traverses the first links until it finds loop or reaches Philosophy (or some threshold # of hops, say 1000). It ran for one night and gave me this loop:

Republic of India > Official names of India > Names of the Republic of India in its official languages > Republic of India (loop)


Thursday, May 26, 2011

Why do people latch doors without lock?

I often see people pulling the latch of a padlock without actually putting in a lock. This is usually done when the person intends to come back after a short time. However I have always wondered about this practice.

The only plausible reason I find for doing this is preventing infants, toddlers or small animals from entering the room. However, in places where I observe this behavior (hostel and department) none of these can be the reason.

Another reason might be to show the immediate unavailability of the person who is usually behind the door. However a latched door without a lock is a clear sign that nobody is there in the room and to a thief this might be an opportunity.
 

Friday, May 20, 2011

SSH without password

Ever since I joined IISc, ssh-ing into other machines seems to be a daily task. Its hard to type in passwords every time. So this is what I did to avoid typing passwords for ssh.

Lets call the machine you are connecting from as "home" with your user name "homeuser" and the machine you are connecting to as "server" with your user name as "serveruser". Please note that in case the account in your "home" computer is compromised, the attacker can effectively login to the "server" as well. So use this with caution.

First you have to generate an RSA public-key, private-key pair. The public key will be placed in the server computer so that it can identify your computer when you ssh to it. Thankfully Linux installations come with all the tools that you require to do this. So all you have to do to generate this key pair is open a terminal in "home" and type:
homeuser@home:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/homeuser/.ssh/id_rsa):
Created directory '/home/homeuser/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/homeuser/.ssh/id_rsa.
Your public key has been saved in /home/homeuser/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:69:bf:89:0a:37:16:e9:25:e2 homeuser@home
Next you have to store the public-key in "/home/homeuser/.ssh/id_rsa.pub" to "server". For this first create a directory on "server" by ssh-ing to it: (don't worry if the directory is already there):
homeuser@home:~$ ssh serveruser@server mkdir -p .ssh
serveruser@server's password:
Next copy the public key from "home" to the "server"
homeuser@home:~$ cat .ssh/id_rsa.pub | ssh serveruser@server 'cat >> .ssh/authorized_keys'
serveruser@server's password:
Now you are done. Try ssh-ing to the server and you will be directly greeted with a prompt:
homeuser@home:~$ ssh serveruser@server
serveruser@server:~$
Please note that:
  • This method kind of identifies your "home" computer to the "server", so in case your account in "home" is compromised, immediately delete the corresponding key from .ssh/authorized_keys at the "server".
  • You have to do this every time you install a new OS in "server" or "home".
  • I have only tested this on the machines that I use ... so this should work well in Fedora, Ubuntu and Mac. Sometimes your version of ssh might differ and you might have to put the public key to ".ssh/authorized_keys2" and change its file permissions to 640 and change file permissions of ".ssh" to 700.
Thanks for reading through. If you have any further questions/comments, just post it here. 

Edit [2011-09-30] For all the lazy people (like me) out there, here is a simple one liner that will do the same trick. Just type in the server password twice when the terminal asks.
ssh-keygen -t rsa; ssh-keygen -y; ssh serveruser@server mkdir -m 700 -p ~/.ssh; scp ~/.ssh/id_rsa.pub serveruser@server:~/.ssh/authorized_keys
 This simple and very effective one-line idea was given to me by my colleagues at amazon.

Tuesday, May 17, 2011

Cell array to matrix in Matlab

Having used Matlab for machine learning work, I often face this problem. Typically while working with some classifier like "TreeBagger", the "eval()" function for computing labels of test data returns a cell array where each element is '1' or '-1'. The problem is that my original labels were numeric {1,-1}, so comparing these become a big problem especially if one is interested in things beyond accuracy.

Here is a simple technique to convert this cell array to a matrix. Say the original cell array is:
cellArr={'-1'; '-2'; '3' ;'6' ;'5'};
Now to convert this into a numeric array, all you have to do is:
mat=cellfun(@str2num,cellArr);
This calls the function str2num for each element of the cell array and returns the resultant matrix in numeric format.

Tuesday, May 10, 2011

replaceTab, an utility program

I use PasteBin for all my online code snippets. However there is one small problem which I find about their service:
Tabs expand to 8 spaces
While many might not understand the fuss, I find this quite annoying. I use Tabs for my code and the default number of spaces I use  for Tab is 4. Expanding Tab to 8 spaces, makes my code look ugly (kind of stretched). Being a programmer, I wrote this following piece of code in C which replaces the Tab by as many characters as one wants.

I designed the program to read and write to stdin and stdout so that I can use it as a pipe for further programs (mostly to facilitate joining multiple files). The syntax is simple:
replaceTab 4 < inpFile > outFile
The first argument is the number of characters I need per Tab. The default value is 4 which is assumed when there are no arguments.

Here is the C source code for the same (yes, I have posted it in PasteBin):



PS: Before writing the code, I tried using the unix command line utility "tr" for this, but could not get it to replace one Tab by multiple spaces. However as it was pointed out to me by a friend this is simple using "sed". The corresponding command is: "sed 's/\t/    /g' < inpFile". ... Looks like I should be spending more time learning Unix utilities than writing simple programs.