Sunday, June 21, 2009

Shuffle functions

Very frequency asked question is "Write a program to shuffle a set of cards". There are many approaches to solve this problem.

Almost all of them treat the set of cards as a list and write the program to shuffle the list.

One approach is to emulate the human shuffling:
Hindu Shuffle; Riffle shuffle

Other techniques are discussed in more details in wikipedia

Here is an interesting but not optimal way(O(n lgn)) to shuffle a list:
In Ocaml:
let shuffle l1 =
let sortComparator x y =
match ( 3) with
0 -> 0
| 1 -> -1
| 2 -> 1
| _ -> failwith "Error in shuffle"
List.sort sortComparator l1

C++ Code:

template < class T >
bool sortComparator(T x, T y){
if(rand % 2 == 0)

template < class T >
vector < t > shuffle(vector < t > cards) {
sort(cards.begin(), cards.end(), sortComparator);
return cards;

Friday, June 19, 2009

Kindle DX review

1. PDF capability:

For pdf documents, treat Kindle DX as a READING device and nothing else.

Does not Support: Note-taking, highlighting, zooming(both the document and font ) capability and has bad searching capability. Also there is no dictionary for PDFs

But it serves the purpose of READING - Resolution is good, it does not strain your eyes, and landscape view is good enough for most of my documents. Also, I prefer to take active notes rather than just higlight, it's not a big deal for me.

Transferring PDF - very easy ... just connect to USB and copy your PDFs to Kindle.

Steve Bain has more critical review on it.

2. Weight:

Extremely light atleast for me.

3. Durability:

Though material looks good, please don't expect it to be extremely sturdy. Hence I recommend using cover(either leather or neophryne) while traveling.

4. Battery life:

Extremely good. I haven't tested it very much but just turn off whispernet and you should expect 4 weeks of battery life

5. Choices of Books:

Amazon is undoubtedly the best choice.

6. Awesome feature:

Wikipedia is free .... anywhere in US !!!

7. Cost:

If you think $359 is OK for Kindle 2, Kindle DX is definitely worth $489.

If you donot like to read research papers on computer, you will save on great deal of printing and "trees" :)

As an engineering student, you will save a huge amount of money and space on printed books if you chose to buy Kindle DX.

Bottom line:

If you cannot live without highlighting and note-taking, DON'T BUY KINDLE DX ... go for Sony PRS-700(if you like text notes) or iRex Iliad(if you absolutely cannot live without handwritten notes)

If you think iRex is too costly (apart from the fact it has no support in US, you have to ship iRex to Germany) and it you want to READ pdfs without straining you eyes, definetely go for Kindle DX.

Thursday, June 18, 2009

Swine Flu

CDC has announced H1N1 flu as pandemic disease and my parents (back in India) were worried. Here are the few facts(as of June 12, 2009) that might ease their concern:
1. Respiratory mask are advised only to people with high risk of influenza (See table 1 at
Most of young college graduates do not fall into this category (unless you are studying health care or related fields). See

2. Number of confirmed or probable cases: 17,855
Number of Deaths: 44

Well, if you are an Indian, I guess you are pretty much immune to it (See Statistics of Malaria for India)
number of deaths | 9352 deaths per year
cause of death probability | 1 in 1110 ~~ 0.090%
rate of death | 1 death per 100000 persons per year
DAILY | 843 738 life years lost per year
DAILY rate | 80 life years per 100000 persons per year
For US, Statistics for Malaria are:
number of deaths | 3 deaths per year
cause of death probability | 1 in 768898 ~~ 0.00013%
rate of death | 0.0011 deaths per 100000 persons per year
DALY | 57 life years lost per year
Well, you can see statistically if you were not paranoid about fever or malaria or cold/cough in India (or US), you need not be worried about Swine flu either.

However, here are prescribed precautions by CDC for Swine flu:
  • Cover your nose and mouth with a tissue when you cough or sneeze. Throw the tissue in the trash after you use it.
  • Wash your hands often with soap and water, especially after you cough or sneeze. Alcohol-based hand cleaners are also effective.
  • Avoid touching your eyes, nose or mouth. Germs spread this way.
  • Try to avoid close contact with sick people.
  • If you are sick with a flu-like illness, stay home for 7 days after your symptoms begin or until you have been symptom-free for 24 hours, whichever is longer. This is to keep from infecting others and spreading the virus further.
  • Follow public health advice regarding school closures, avoiding crowds and other social distancing measures.
Reference: Statistics are gathered from Wolfram Alpha and CDC