Productive Meter

A few weeks ago I decided that I should suck it up and start learning how to develop for the web. After asking around, my faithful community brethren, I decided to learn Django from its docs

::Django documentation is awesome::

Around this time I came across this post about Waking up at 5am to code. I tried it a few times and it worked wonders. I've been working on a small project that can keep track of my productivity on the computer. The concept is really simple, just log the window that is on top and find a way to display that data in a meaningful way. 

Today's 5am session got me to a milestone on my project. I am finally able to visaulize the time I spend using a decent looking graph. Which is a huge milestone for someone who learned how to display html tables 3 weeks ago.

Tools:

A huge thanks to my irc friends and random geeks who wrote awesome blog posts and SO answers on every problem I encountered.

I will be open-sourcing the app pretty soon. Stay tuned.

Too Many Classes Too Little Time

I'm taking a couple of the free online classes offered by Standford. One on Artifical Intelligence and one on Machine Learning

I haven't had so much fun since kindergarten. Actually that's not fair, I didn't enjoy kindergarten this much. I'm listening to the classes during my lunch, after work, during weekends. I'm working on my assignment with so much enthusiasm, I dread the day when this class ends. 

Stanford just announced a slew of new online classes offered starting in Jan 2012. I was way too excited when I first read the description on them. Now I'm a little sad, becasue I want to take 8 out of the 11 courses that are being offered and I don't have enough time. :(

Woe is me. 

ps: If you are not taking any of these classes you are missing out big time. Please do yourself a favor and sign up. 

Picking 'k' items from a list of 'n' - Recursion

Let me preface this post by saying I suck at recursion. But it never stopped me from trying to master it. Here is my latest (successful) attempt at an algorithm that required recursion. 

Background: 

You can safely skip this section if you're not interested in the back story behind why I decided to code this up. 

I was listening to KhanAcademy videos on probability. I was particularly intrigued by the combinatorics video. The formula to calculate the number of combinations of nCr was simple, but I wanted to print all the possible combinations of nCr. 

Problem Statement:

Given 'ABCD' what are the possible outcomes if you pick 3 letters from it to form a combination without repetition (i.e. 'ABC' is the same as 'BAC'). 

At first I tried to solve this using an iterative method and gave up pretty quickly. It was clearly designed to be a recursive problem. After 4 hours of breaking my head I finally got a working algorithm using recursion. I was pretty adamant about not looking it up online but I seeked some help from IRC (Thanks jtolds). 

Code: 

def combo(w, l):
        lst = []
        if l < 1:
            return lst
        for i in range(len(w)):
            if l == 1:
                lst.append(w[i])
            for c in combo(w[i+1:], l-1):
                lst.append(w[i] + c)
        return lst

Output:

>>> combinations.combo('abcde',3)
    ['abc', 'abd', 'abe', 'acd', 'ace', 'ade', 'bcd', 'bce', 'bde', 'cde']

Thoughts:

  • It helps to think about recursion with the assumption that an answer for step n-1 already exists.
  • If you are getting partial answers check the condition surrounding the return statement.
  • Recursion is still not clear (or easy). 

I have confirmed that this works for bigger data sets and am quite happy with this small victory.

Python Profiling

I did a presentation at our local Python User Group meeting tonight. It was well received, but shorter than I had expected. I should've added a lot more code examples. 

We talked about usage of cProfile, pstats, runsnakerun and timeit. 

Here are the slides from the presentations: 

The slides were done using latex-beamer, but I wrote the slides in reStructuredText and used rst2beamer to create the tex file which was then converted to pdf using pdflatex. 

The source code for the slides are available on github.

Programming - A Gateway Drug to Math

I decided to try my hand at the Stanford's AI Class. The pre-requisites mentioned Probability and Linear Algebra. So I started watching Probability videos on KhanAcademy

Sal Khan was teaching how to find the probability of 2 heads when you toss a coin 5 times.

A classic nCk problem: 

The probability of getting 2 heads while tossing a coin 5 times is:

But I wanted to find out the probability of getting at least 2 heads when I toss 5 coins.
Its really simple. All I had to do is P(2) + P(3) + P(4) + P(5). 
But then computingby hand (or a calculator) was painfully slow, let alone do it 4 times.
So I wrote two little functions in Python that will calculate factorial (yes I reinvented the wheel) and
Nothing teaches you math faster than trying to write a program to do the math for you. 
Writing a program is the same as teaching the computer how to do a certain task. The only way you can teach someone to do a task is to become a master at doing that task yourself.

Bonus: It also teaches you corner cases like 0! = 1 and  that you wouldn't think of otherwise.