September 30, 2008

Python - StackOverflow Fight

I've been having fun using the new StackOverflow site for answering technical questions.

Here is a Python script I call 'StackOverlow Fight' (like Google Fight). It takes a list of tags and gets the count of questions that are tagged with each one.

As an example, here is StackOverflow fight between some popular programming languages:

#!/usr/bin/env python

import urllib
import re

langs = ('python', 'ruby', 'perl', 'java')

url_stem = ''

counts = {}
for lang in langs:
    resp = urllib.urlopen(url_stem + lang).read()
    m ='summarycount.*>(.*)<', resp)
    count = int(',', ''))
    counts[lang] = count
    print lang, ':', count
sorted_counts = sorted(counts.items(), key=lambda(k,v):(v,k))
print sorted_counts[0][0], 'wins with', sorted_counts[0][1]


python : 733
ruby : 391
perl : 167
java : 1440
java wins with 1440
Post a Comment