Favourite Quote :P

Justifications are the way of the world
Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Friday, April 1, 2011

Sorting a list of complicated tuples in Python

Consider a tuple of the form:

("URL of page", "Title of web page", ['word1' ,'word2', word3'], len(words), priority index - int)

where word1, word2 etc are the words in the web page, the priority index is an integer representing the priority of the web page.

We have a list of such tuples.
Example:
[
("www.yahoo.com", "Yahoo! - International", ['yahoo', 'mail', 'news', 'sports', 'messenger'], 5, 2)
("www.google.com", "Google Web search", ['google', 'mail', 'search', 'docs'], 4, 1)
("timesofindia.indiatimes.com", "Times of india - Latest news", ['timesofindia', 'news', 'india', 'sports', 'world'], 5, 3)
...
...
]

We are to sort this list based on the following criteria (in the order given):

  1. Length of words in the words list
  2. Priority index
  3. Alphabetical order of titles

Simplest way would be to make the tuples conform to this format:
(priority index, length of words, title , ... )
Python would by default sort the list using elements from the start to end when one uses list.sort() or sorted(list)

But  what if we wanted the sorting to be done so that the numbers are sorted in the descending order, while the titles are sorted in the ascending order?
Here's when a custom sort function like the one below would prove useful:

def tupleSort(t1, t2)
    #first sort according to word length
    t1wordlength = t1[3]
    t2wordlength = t2[3]
    if t1wordlength >t2wordlength : return 1
    elif t2wordlength >t1wordlength : return -1
    #next sort according to the priority index
    t1priority = t1[4]
    t1priority = t2[4]
    if t1priority >t2priority : return 1
    if t2priority >t1priority : return -1
    #finally if both of the above are same, then sort according to the alphabetical order of the title
    t1title = t1[1]
    t2title = t2[1]
    if t1title >t2title : return -1
    if t21title >t1title : return 1
    #if even that is same then the tuples match, so return 0
    return 0

Giving this funtion as the parameter to the cmp parameter of the list.sort() function of sorted() BIF with reverse parameter set to True would cause the list given on top to be sorted in the way we want.

Saturday, March 26, 2011

What hooks programmers

The joy of programming is the joy of building and creating something. Wouldn’t it be amazing if we can build and create something useful for other people? If yes, why aren’t we doing more of that? After all, there is no dearth of things that we can create.
Courtesy: http://www.swaroopch.com/blog/how-fresh-graduates-can-grow/

Personally, I feel coding is the fastest way to see your creation in action. As best as worldly creation gets :)

Saturday, January 15, 2011

Programming?? Whoa!!

This one day I was trying to explain the concepts of computer programming to a civil engineering student. I was so lost ...  this girl wasn't getting anything at all. That was when I hit upon a new way of looking at programming. It is simple problem solving.

Consider this example. You are given a problem: 


What you have are the following:

  1. A list of numbers represented as myArray(1, 2, 3, 4, 5)
  2. You have only a certain words (and characters) to express yourself:
  3. while
  4. print
  5. "(" and ")"
  6. "{" and "}"

 etc .. (You would only need a few more).
Write down the steps to find the sum of all the numbers in the list, given certain ways of defining which steps are to be executed multiple times and so on.

If programming had been explained like this to me when i first learnt it, it might be that I would have looked at programming in a totally different light. :)