Posts Tagged ‘programming’

Analyse Me

May 2nd, 2010

A few weeks ago, a couple friends and I decided to host a web app jam weekend.

That was this weekend, here’s what my team came up with: Analyse Me is a chrome extension that helps you figure out where you’re spending the most time on the internet.

I’ll have more on the web app jam soon.

Russian Peasant Multiplication

July 26th, 2009

Interesting way to multiply numbers by hand.

This is a simple python program that takes 2 arguments and shows the steps of this multiplication technique.

#!/usr/bin/python
from os import sys
def russianPeasant(num1, num2, cache):
   cache[num1] = num2
   if num1 == 1:
      return
   return russianPeasant(num1>>1, num2<<1, cache)

def printResult(cache):
   sum = 0
   for key in reversed(sorted(cache.keys())):
      if key%2 == 1:
         sum += cache[key]
         print(str(key) + ': ' + str(cache[key]))
      else:
         print('' + str(key) + ' ' + str(cache[key]) + '')
   print("------------\n" + str(sum))

cache = {}
russianPeasant(int(sys.argv[1]), int(sys.argv[2]), cache)
printResult(cache)

Losing your identity in the tubes with google

December 25th, 2008

5 Years ago, when I googled myself for the first time, the main result that came up was “Charles M.A. Stine”, a material engineer who made explosives. Today, I find 52,900 results on google with unique people ranging from a dentist, an Inventor, an associates, an IT manager and about 20 others here. Indeed names are too common a tool for identification when you have more than a few hundred people not to mention the hundreds of millions on the internet.

Randall Munro originally used the 4 letter string ‘xkcd‘ to uniquely identify himself on the internet before his webcomic became famous. I though, “maybe it’s time I found a short meaningless unique identifier too”. I wrote a script to try and do just that:

import urllib
import re
#returns the string representation of the number
#note that strings of the form [a]+ will be skipped since numbers of the form [0]+ is equivolent to 0
def numToStr(num):
   str = ''
   while num <> 0:
      l = num%26
      str += chr(97 + l)
      num = num/26
   return str

#the page will have this string when no results are found
schstr = 'did not match any documents'
url = 'http://www.google.com/search?q='
for n in range(26**3, 26**4):
   str = numToStr(n)
   f = urllib.urlopen(url+str)
   for line in f
      if re.match(schstr, line):
         print str
         exit(0)
   f.close()

But evidently that’s against googles terms of service section 5.3

Google Error when doing searches with a script

5.3 You agree not to access (or attempt to access) any of the Services by any means other than through the interface that is provided by Google, unless you have been specifically allowed to do so in a separate agreement with Google. You specifically agree not to access (or attempt to access) any of the Services through any automated means (including use of scripts or web crawlers) and shall ensure that you comply with the instructions set out in any robots.txt file present on the Services.

I’m still looking for an answer: What is the shortest english alphabetic string for which google returns no search result. Of course the answer to this question this can’t be googled because as soon as the answer to this question ends up on googles search results, the result becomes invalid.

Writing and Programming

November 12th, 2008

This post is inspired by Jeff Atwoods post “Coding: It’s just writing“  Jeff argues that like writing, to code effectively is to write clear concise code.

When you’re writing on a personal blog. Very little thought goes into what you write. Much of the time, it’s almost a stream of consciousness, it’s whatever comes to your mind at the time of writing. Unless you’re a natural and experience writer with an expertise on the subject on which you’re writing, your readership will never reach an audience beyond your social circle. When you write professionally, or at least outside the personal blogging bubble, you expect to have a bigger audience, or at least an audience with higher expectations of the quality of your writing. You take more time with the choice of words, the choice of topics, the sentence structure, you have to think about what you want your audience to get out of it, you think about what your audience would like to read and whether they would like to read more of your writing afterwards. When we are reading someone elses writing, whether it’s a book, a review of a movie, or an article in the news paper, we expect certain standards or properties from each piece of writing. We unconsciously expect that the authors have considered their audiences when writing, and hope that we’re part of the audience that will find it interesting, relevant and useful.

Similarly, when you write code in the real world, you are writing it for others to read. You have an audience, those who will be maintaining your code. When we inherit someone else’s code, we hope that the authors have considered their audience and wrote code clear enough for you to understand and maintain. You hope that they have built a framework based on best practice design patterns, and that you can add to it or change it without too much effort.

When you work on a pet project, most of the time, you’re the only person who is going to read the source code, and it’s okay to take shortcuts, write ‘hacky’ code, and provide little to no internal documentation. For any non-programmers sad enough to be reading this blog(get a life!), imaging building a house and realize only after you are nearly finished that you forgot to build a bathroom. When a programming faces a analogous situation, he/she would have to write ‘hacky’ code to get around the problem by perhaps using the kitchen as a bathroom because it has similar pluming.

When you work on bigger projects, you have to think about the overall design of the system you’re building, otherwise it gets too complex to manage and you’ll be forced to use hacky programming tricks to fix and change things that will confuse those who will be reading your code later. (Imaging living in and maintaining a house with a kitchen that doubles up as a bathroom, how awkward!) When you write a book or a major essay, you have to do some top down design. It has to have some structure and planning, otherwise you’ll confuse your audience and the ideas you’re trying to present constantly changing as your mental state changes while writing.

I started blogging because I wanted practice in writing. As much as I hated English class in high school, I miss the creative writing essays — essays which I very much enjoyed writing. I chose to study computer science because I thoroughly enjoyed my first programming class in high school. Yet I’ve always thought they were very different activities. One requiring creativity, the other requiring strict logic and analysis. Only after reading Jeff’s post did I notice the similarity between the two. I’m not a natural writer, and I wouldn’t call myself a natural programmer, but I certainly find the latter much easier. Perhaps with this new realization, I can learn to write and code better at the same time.

The elements of style (a book on effective writing that inspired Jeff’s post that inspired mine) has been added to my to-read list. I’ve probably violated many advices that this book will recommend about writing, but this is still a personal blog with almost no audience base and I can still write things without much thought if I wanted to :)