lord abhi…

November 17, 2009

Java Sort By Map Value

Filed under: Java, Programming, Software — Abhishek Khaitan @ 11:58 pm
Tags: , , , , ,

Java utility method to sort a Map based on map values.

@SuppressWarnings(value = { “unchecked” })
public static List sortByValue(final Map m) {
List keys = new ArrayList();
keys.addAll(m.keySet());
Collections.sort(keys, new Comparator() {
public int compare(Object o1, Object o2) {
Object v1 = m.get(o1);
Object v2 = m.get(o2);
if (v1 == null) {
return (v2 == null) ? 0 : 1;
} else if (v1 instanceof Comparable) {
return ((Comparable) v2).compareTo(v1);
} else {
return 0;
}
}
});
return keys;
}

November 26, 2008

String Reverse in Python

Filed under: Programming, Python, Software — Abhishek Khaitan @ 8:04 pm
Tags: , , , , ,

One line code to reverse a string in Python:

strOrig = “xyz”

revStr = s[::-1]

where ’strOrig’ is the source string and ‘revStr’ is the string reversed.

Python is indeed powerful!!

October 19, 2008

Python Prime Number Generator (Sieve Algorithm demystified)

Filed under: Programming, Python, Software — Abhishek Khaitan @ 1:45 am
Tags: , , , , ,

#The prime number generator is using Sieve algorithm for fast prime number computaion. The following are the steps of the algorithm:
#
#Algorithm:
#==========
#
#   Step 1. Consider a contiguous list of numbers from two to some maximum. (This is the list of squares on the left side of the picture.)
#
#   Step 2. Strike off all multiples of 2 greater than 2 from the list.
#
#   Step 3. The next highest, uncrossed off number in the list is a prime number.
#
#   Step 4. Strike off all multiples of this number from the list. The crossing-off of multiples can be started at the square of the number, as lower multiples have already been crossed out in previous steps.
#
#   Step 5. Repeat steps 3 and 4 until you reach a number greater than or equal to the square root of the highest number in the list; all the numbers remaining in the list are prime.
#
#Source: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

import math

def primeNumGenerator(input):

#Here if the input is less than 2, then return an empty list since the smallest prime number is 2
if input < 2: return []

#If input is 2, then return 2
if input == 2: return [2]

# Steps 1&2 of the algorithm – We are here considering a contiguous list of number from 2 to given imput. Setting step of 2 in the range function strikes off all multiples of 2 greater than 2 from the list
s = range(3, input, 2)
mroot = math.sqrt(input)
listofinput = len(s)
i = 0
# Step 3 of the algorithm. We start with number 3 in the list as we have already considered scenarios till number 2 in the list above
m = 3

#As per prime number generator theorm, the numbers in the list are only checked till the square root of the number for which the prime number series is to be generated
while m <= mroot:
if s[i]:
#Step 4 of the algorithm. To strike off all multiples of this number from the list, we need to first calculate the index of the multiple in the list.
#The formula for doing so is (m*m-1). But since we are considering only odd number series, the formula gets modified to: (m*m-1)/2.
#Now since our odd number series does not have 1 (or has a member missing), the formula further gets modified to: ((m*m-1)/2)-1 = (m * m – 3)/2
#Also floor division is used here (//) since only the integer part is needed for index
j = (m * m – 3)//2
#  Step 4 of the algorithm continued. Striking off multiple of this number
s[j] = 0
#   Continuing to striking all other multiples of this number from the list
while j < listofinput:
s[j] = 0
j += m
#  Increasing the index to process with the next number in the list
i = i + 1
#        Now the nth element in a odd number series is (2n+1) where n is the index. Since our list starts with 3, the formula gets gets modied to:
#        2(m+1)+1 = 2m+3 which gives us the next number in the series
m = 2 * i + 3

#   Using list comprehension to build the prime number series. First we are adding 2 to the series since we didn’t consider 2 in our series are 2 is prime. Now all the indexes not marked as 0 are the prime numbers
return [2]+[prime for prime in s if prime]

if __name__ == ‘__main__’:
num = 20
primeNumberList = primeNumGenerator(num)
print “List of prime numbers from 2 to < %d:” % num
print primeNumberList

June 23, 2008

Zen Of Python

Filed under: Software — Abhishek Khaitan @ 5:33 pm
Tags: , ,

To see the ‘Zen Of Python’, which is widely accepted as the principles on which the python is based, simply type ‘import this’ at the Python prompt.

C:\>python
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on
win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!
>>>

June 5, 2008

How to download video from YouTube

Real Player 11 though as not much popular these days, did strike back big with cool features with its version 11 allowing downloading streaming video.

Here is how you can download videos from YouTube.

Download RealPlayer 11 for your Operating System. You can use the following link to download:

http://www.real.com/freeplayer/?rppr=rnwk

After downloading the file, start to installation by double clicking on the executable file and following the installation steps. When you come to the installation step (as shown below), make sure to check the check box for “Download & Recording”. Continue with the installation process.

After installation is finished, open the Real Player. Go to Tools->Preferences. Click on the “Download & Recording” tab from the “Category“list on the left hand side of the window (as shown below)

Select “Only on mouse-over” radio button. You can also change here the directory path where you want the downloaded video to be saved. Click the “OK” button. Close the internet browser and then again reopen.

Now to download the video from the YouTube, start to play the video you want to download on YouTube. Point the mouse cursor to the top right corner of the screen and click the download button.

The download of the gets started. Real Player can download multiple files simultaneously. The file is saved with “.flv” extension which can be later player by RealPlayer or VLC. Happy downloading :-)

May 24, 2008

How to install Evernote 3.0 on Ubuntu

Evernote allows you to easily capture information in any environment using whatever device or platform you find most convenient, and makes this information accessible and search able at anytime, from anywhere. With the new version of Evernote (still in beta) not only can one download the software but can also work online and later synchronize Of all the available notes solution available in the market, Evernote is one of the most desired software application for taking down notes, pictures, videos, etc. But the bad news for Linux/Ubuntu users is that Evernote installable is only available in Windows XP/Vista and Mac. There is no Linux support.

But using wine I was easily able to install Evernote on my Ubuntu. The steps are as follows:

If you do not have wine installed, you can get it with the following command:

> sudo apt-get install wine

Download the Windows installation executable of Evernote from the Evernote site (http://evernote.com/about/download/)

After wine is install, type the following command:

> wine location where the executable is downloaded

For example, in mine case:

> wine /home/abby/apps/Evernote_3.0.0.594.exe

The installation process starts which is similar similar to Windows.

Complete the installation steps and you will have Evernote installed on your Ubuntu. There shall be a icon to start Evernote on the desktop too after installation is complete.

Since Evernote is in Beta, to get the invitation one need to give there  email id at this link (http://www.evernote.com/about/prereg/)and they shall send you the invitation to register. I have 18 invitations remaining. In case you need one, do let me know.

Now no reason to be sad since your favourite Evernote works with your favourite Ubuntu :-)

Blog at WordPress.com.