Saturday, August 06, 2011

Organizing clothes using Data Structure

Due to lack of hanger space in my present wardrobe, I have to keep my clothes folded. I usually pick up something from the top of the pile for wearing. Which means I keep wearing the a small subset of my clothes repeatedly. Ideally I would have liked to wear all the clothes. But its too much trouble to take a tshirt out from middle of a pile; the entire pile gets disheveled.

The solution is actually quite simple. Think of the tshirt pile as a stack. What I want is basically a queue. So all that is left to do is to somehow "implement a queue using a stack". The CS grads have already faced this problem in their Algorithm and Data Structure courses. I applied the same technique.

I used two stacks, one for putting the washed clothes in (enqueue) and another for taking out the clothes for wearing (dequeue). If the pile for wearing is empty, [and here comes the important step] then turn the washed clothes pile upside down and keep it in place for the wearing pile.

Its good to know that Data Structure knowledge is useful beyond computers too !!

Saturday, July 16, 2011

Confessions of an Ebook Pirate

Today I found this article, titled "Confessions of a Book Pirate". Its an email interview with a bibliophile who is also a book pirate. The person in question has a name "The Real Caterpillar" in one of the many book sharing communities that he participates. He makes quite a few points which seemed logical and worth noting. The article is a definite recommend from me for anybody who loves books and is aware of ebooks and piracy.

However, some of the arguments seem feeble. Like "I would probably still grab a book if I stumbled across the file and thought it might interest me – or if I wanted to check it out before buying a paper copy". Well both of these can be very well accomplished in Google Books (partial book) or in the local bookstore (full book).

Personally, some of my interests fall into pretty much the tail end of the curve; and often finding books on some of these topics are quite difficult. Sometimes the books themselves are out of print. But I guess the ebook industry is moving towards the correct direction. With ebook readers flooding the market and big players like amazon coming in, bibliophiles like me are having a good time. Still it will take quite sometime till the entire spectrum is covered.

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.