Sunday, October 21, 2007

Downloading Pages via SSL and proxy using LWP Perl

If you use LWP modules in perl then you can download pages via HTTP easily using a proxy. However if you wanna download pages with HTTPS through a proxy then it becomes a problem.
This is because both proxy and HTTPS are not implemented simultaneously in LWP modules although individually they work quite well.
But there is a way around. To download paged via HTTPS you have to give LWP the impression that you are not using any proxy. Thus you set the environment variable HTTPS_PROXY as a valid proxy and dont set anything as the proxy in the request object for https. This would make it work like a charm. :)
Link to above Funda.

Thursday, June 28, 2007

Recovering deleted files in an ext3 File System

I was working on my Debian machine few days ago when accidentally I pressed "rm -rf *" in a wrong directory(my main working directory). Well, I lost all the data. And after hunting net for hours, find out that there is no traditional method available for recovering files in ext3 file system.
In Ext2 file system, OS marks the blocks, inode and directory entry associated with the deleted file as unused. It did not use to delete the links, so that other files could still use them. And because of this, recovery of lost files were possible using certain tools like debufs, e2undel, recover, Midnight Commander etc. Giis-gET iT i sAY also claims to recover files from both ext2 and ext3 file system but I don't know it works or not.

In Ext3 file system, when the file is deleted, file size and block addresses in inodes are also deleted. Therefore, its not possible to find out where the file was originally located or more specifically it becomes difficult to track back the inode of the file using debugfs.
But, if you are lucky, file could still be there in your hard drive, though in chunks. Steps you must follow -

  • Do not save any file in the same partition in which your deleted files were. This might result in overwriting of occupied blocks of deleted files.
  • Mount another drive to the system(if not mounted) to save the dump.
  • Find the Partition in which deleted files was there using `mount -t`
  • If you know certain lines from the file, which are more or less unique to the file you deleted, then it might be useful. In my case, my Perl code began with "package rankSentences;", so I used that as the identifying text.
  • Try the command =>
grep -a -B[lines before] -A[lines after] ‘identifyingText’ /dev/[partition] > dump.txt

here,
  • -a is for treating text file as if it is binary file.
  • -A Number of lines of the dump that you want to extract after the "indentifyingText" (which was "package rankSentences" in my case.
  • -B Number of lines before "identifyingText" you want to extract.

If you don't specify "identifyingText" this might result in dump.txt to be very large in size as it will start dumping the data of complete partition to that file.
And ya, be patient, it might take some time depending upon the size of your partition. After running that command you can look for chunks of your deleted file in dump.txt. And if you are lucky, you might find the complete file at one place. :)

--compiled from various sources on net.

Friday, June 08, 2007

Recovering Lost Emails from Google Desktop Search's Cache

This post is for you if you
  • Have accidentally deleted mails and
  • Use Google Desktop Search (GDS) and
  • Were fortunate or sensible enough or possibly farsighted [:)] to have enabled email caching in GDS and
  • Want to retrieve your mails

Andrew Hardwick has written a very simple and useful script for the above task. His script was tested on GDS version 2 (build 20051208-en). Refer to the instructions given on his page to get the details of the script.

I recently needed to do similar task for someone who was using Google Desktop 5.1.0705.04505-en-pb. The above script worked just fine with slight modifications in the regular expressions. I am uploading the modified script in here and a copy of Andrew's Instructions here.

Enjoy !!

Friday, January 05, 2007

Stock Index

What does the stock Index indicate?

First, let’s look at what an index number represents. Although there are different ways to calculate index numbers, it is important to remember the numbers represent a change from an original or base value.

The number is not important. What is important is the percent change over time. This movement up or down gives you and idea of how the index is performing. Is the Dow up or down? The index is calculated “on the fly” during trading to give investors a sense of direction to the market it represents

The sensex's calculation involves dividing the total market capitalisation of 30 companies in the index by the index divisor — the only link to the original base period value of the sensex.

These 30 companies are taken as the representative of the Market. There may be more than 30 companies registered in the exchange.


Spell Checkers

Ever Wondered how search engines like Google, Yahoo or for instance Microsoft word Does spell checking.

Well the basic idea is to get Levenshtein or Damerau-levenshtein distance between the mis-spelled word and the other word. But do we find the distance with all the other words that are in the vocabulary. The answer is No. We reduce the subset of the words to checked. In the Search Engines it can be the most recent or the most used words and there are other techniques to do so.

Some insight into Google spell checking.
Spelling Suggestion at Wiki.

Tuesday, January 02, 2007

Casting Out Nines

How do you check the answer of a large multiplication like 8216*4215=36430440 without actually doing the multiplication.

Sum 8+2+1+6=17. Take modulo 9 = 8.
Sum 4+2+1+5=12. Take modulo 9 = 3.

Sum 3+6+4+3+4+4=24. Take modulo 9 = 6.

Now check 8*3=24 and 2+4 = 6.

Austrian Method of Subtraction

How do you subtract 5592 from 11149. Simple question....

There's an interesting method which involves only addition as it is easier to do than subtraction. Add from the amount to be subtracted upto the next higher unit, then to the next and so on.

Example 5592 + 8 = 5600. 5600 + 400 = 6000. 6000 + 5000 = 11000. 11000 + 149 = 11149.
so the answer is 8+400+5000+149 = 5557.

Pascal's Triangle

There's a very good article about Pascal's Triangle in Wiki. I am just enumerating some important points for quick reference.

- Construction of Triangle, value of the element is equal to the sum of the above two adjecent values.

- Sum of the elements of the mth row is equal to 2 raised to power m-1.

- Elements of mth row correspond to coefficients of 11 raised to power m-1.

- Generally the elements of mth row are the coefficients of the binomial expression (x+y) raised to power m-1.

- The diagonals next to the edge diagonals contain the natural numbers in order. Moving inwards, the next pair of diagonals contain the triangular numbers in order.

- Starting at the top 1, without backtracking or going sideways, try to get to another node via these grid paths as many ways as possible. The answer is whatever number the node has.

- Sum of a shallow diagonal is a Fibonacci's number.

- In rows where the second number (the 1st number following 1) is prime, all the terms in that row except the 1s are multiples of that prime.

- Pascal's triangle can be used as a lookup table for the number of arbitrarily dimensioned elements within a single arbitrarily dimensioned version of a triangle (known as a simplex).

- No one knows whether any number occurs 5 times in a Pascal's triangle.

Monday, January 01, 2007

Hats

There are 5 persons in a line, all facing in the same direction. There are 5 hats, 2 white and 3 black. A person can't see the color of his own hat, but can see the color of the hat of all the people standing in front of him.

The Third/middle person was asked to tell the color of his own hat. He couldn't answer. Then the second person( the person in front of the middle one) was asked the color of his own hat. He too couldn't answer. Tell the color of the Hat of the first person in the line?


===========================================

Solution:

Middle person couldn't tell the color. It means of the two persons standing in front of him at-least one of them was wearing a black hat.

Second person couldn't tell the Hat's color. He knows that there has to be at-least black hat either on him or on the first person. If the first person is wearing white then he is surely wearing black. But if the first person is wearing black, then he can't tell. Thus the first person is wearing black.

Salaries of three persons

There are 3 persons A,B,C and their salaries are X, Y, Z respectively.

Problem: Device a scheme so that all three of them know X+Y+Z, by asking each other questions.

Constraint: None should come to the other one's salary, i.e. A should not know Y and Z and so on...


===============================================

Solution:

C tells B that its salary is K (a fake value)
B tells A the total K+Y
A tells C the total K+Y+X
Finally C calculates X+Y+Z and tells it to everybody.