How I Got My Summer Internship
It was late in the game when I got accepted to UT Graduate School. A lateness I attribute to every other applicant being much more interesting than me but ultimately deciding to instead go on and do something completely amazing.
So I had a week to decide, and everyone had already hired interns. Even my inside contacts couldn’t change that the hiring had already been done. So I resigned myself to the prospects of being unemployed for the summer and started making myself useful. But I jumped on an internship opportunity that popped up.
After a typical get-to-know-you type phone interview, they tossed a programming assignment my way. Certainly more interesting than the “insert this number into a linked list” and “implement a graph search” kinds of interview problems thrown at me before.
The idea I had was to rewrite the django.utils.html.urlize function. From the docs: Convert any URLs in text into clickable links. …
More completely described in the source. So that night, I cracked my knuckles and cranked something out. For comparison:
A snippet from Django’s version.
if middle.startswith('http://') or middle.startswith('https://'):
url = urlquote(middle, safe='/&=:;#?+*')
...
trimmed_url = trim_url(middle)
middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr, trimmed_url)
Same snippet from my version.
if lower(current).startswith(prefix):
url = PREFIXES[pref] + current
...
if url:
result.append(unicode('%s<a href="%s"%s>%s</a>%s'
% (beg, url, ' rel="nofollow"' if nofollow else '', link, end))))
Which prompted a quick response of:
Looks very good — definitely a lot more readable than the Django version. … A quick benchmark shows that your version is twice as fast as Django’s version.
And an internship offer. Now to be fair, my code is doing approximately half the work Django’s is, so it’s expected that mine is twice as fast. But there is an open bug to optimize urlize, so I tack another task onto my todo.
Since “you should always optimize for readability first” 1, it should be much easier to optimize my code than Django’s.