Regexes For Life

An impolite subversion (and Karaoke)

leave a comment »

Because this book, Etsy, Bach, cicu control error, so that the conscience of humanity. Karaoke? If you invest. What is surprising and encouraging. Anti-brain model, not all of the cities and streets – including travel tool. (This is surprising, of course), the efforts of Member.

What is this? And expectations? I am not a very good package, although Google’s technology, we hope that all leaders. First, I would like to, I hope vyhrajete Ⅰ. Yes, car.

Is happening?

In his book Gödel, Escher, Bach, Hofstadter hints at what he believes gives rise to humour in human brains. Humour, he says, is a subversion of expectations. When we are amused, we are also surprised. In a hypothetical model of the brain’s functioning, he attributes this to a trip down an unfamiliar neural pathway- like a tour across a version of your country that has all the city and highway names mixed up. (The latter bit sure doesn’t seem funny to me.)

This post is an attempt at this “subversion of expectations”. I’m no good at engineering such twists, though, so I sought help from the all-knowing Google overlords. I figured that the first person to have his expectations flipped over and kneaded by this exercise ought to be me.

That’s enough hinting, I think. What do you think happened to this piece?

Written by Karthik

June 25, 2009 at 12:26 am

Posted in Experiments

Tagged with

A Short History of Penfight

leave a comment »

Young cousins are a hyperactive lot. In an effort to keep the runts engaged, I improvised a game played with marbles (the only props at hand at the time that could pass for toys). It was a two-minute affair, and the game was chancy, full of loopholes and not likely to amuse anyone for over a few minutes. So I was surprised to see them playing it with religious fervour two years from that boring afternoon.

I mentioned to them in passing that this has happened before, with a classroom game now known as Penfight. The expression on their faces led me to believe that I’d said something profound. I confirmed to their flabbergasted countenances that yes, I did indeed invent this game- at school in Delhi sometime around the latter half of 1997, although I was not the one who named it so.

Apparently, it has grown into something of a phenomenon in Indian high schools- venues otherwise known for inducing ennui, encouraging overt passiveness and for their deadpan deliverance of useless infodumps. As a somnolent sixth grader, I possessed not the enthusiasm for flinging paper balls tied with rubber bands around the classroom, which was what most boys in the class got around to in the absence of a teacher. Watching a classmate trying to attack a half-hammered nail on his desk with his pen, I realized there was pastime to be had here. Soon we were trying in turns to knock each others pens off his desk by flicking them.

Penfight in its original form is a simple game. There are two or more pens on the surface of a table, one per player. Players take turns trying to knock all pens except their own off the table by flicking their pen with their forefingers, much like one propels the striker in Carroms.

As a competitive sport, Penfight was perfect. In retrospect, I realize that it had all the elements of a classic- it required skill, and a fair knowledge of the playing surface’s irregularities. It was easy to get better at, but hard to be really good, as luck was a terrific equalizer. Knocking pens off a table was addictive. There was room for strategy- many of us could gang up against a strong player and then fight it out amongst ourselves. (Facing a much stronger opponent by yourself, you could attempt a kamikaze charge and end the round in a draw.) It required no time to set up and not much energy to play. It was short, perfect for that three minute interval between classes. It scaled well; a single desk could accommodate up to five players (and pens), and you could join adjacent desks to make more room. Most importantly, it was fun!

I didn’t have a name for it- heck, I didn’t know for sure what we were doing. Soon everyone in the classroom was playing it. Tournaments were held, and whatever advantage I possessed owing to an early start was lost. In a few months, everyone was playing better than me, and by the end of the year, people were gambling on the pens themselves. By this point I had given up on the game and taken to reading comics in class instead. (I think this marked the birth of my introversion, a characteristic that’s remained with me since.)

1998 was a different time, and I was at a different place. More boredom struck at my new school in Bangalore, and I decided to try my hand (fingers) at Penfight again. (I still didn’t have a name for it.) I caught hold of a couple of classmates and got them to play with me- and boy, did it catch on! Once again, I lost whatever advantage I possessed in a few weeks. Apparently, everyone had a better intuitive understanding of angular momentum and centers of mass than I. This time around, people started bringing in heavy duty pens to aid their cause- metal dreadnoughts, relics from bygone ages with plenty of ballast; I was well and truly out of the game.

I lost track of what happened to this phenomenon after my tenth grade. By then, I’d heard the name Penfight being tossed around. I’d lost all interest in classroom pastimes, and I’d all but forgotten about it until my cousins (who attend a different school) informed me, twelve years from the first time ever (probably) a round of penfight was played, that their school has had notices put up stating “PENFIGHT DURING SCHOOL HOURS IS BANNED”. Ha! It’s part of the high school student’s, and better yet, the school’s vocabulary now.

It’s not a hard game to cook up- any sufficiently bored enterprising schoolkid could invent something of the sort, so it is quite likely that Penfight was invented at several places around the country at roughly the same time, and that this reminiscence is one of many histories of Penfight. I’m not proud of it- partly because I don’t find it particularly clever, and partly because I’ve always been rubbish at the game.

But yeah, for the record, I (co-)invented Penfight.

If you’ve been through the farcical ordeal that is high school in the past decade, perhaps you have engaged in Penfighting. If you have, what is your Penfight story?

Written by Karthik

June 16, 2009 at 8:31 pm

Posted in Verbiage

Tagged with

Eh?

leave a comment »

me: I finally found my comfort zone in this hierarchy of levels of complexity in which you can use a computer.
Mine’s just below the “application level”, at the level of the tools used to create applications.

Sameer: kya? :-}

me: Basically, there are a whole lot of “levels” at which you can use a PC.
Some of them complex enough to sound nightmarish to anyone- these are the “lowest” levels, sending system calls yourself, etc.

Sameer: well isn’t it relative

me: Most casual users use the highest level,
(GUIs with nice text boxes, etc.)

Sameer: for eg. I can send sys calls and interact with the kernel but on the other hand I can’t sometimes understand how basic applications work :P

me: Well, yes, knowledge of one level does not imply knowledge of all of them.
Refer http://rightshift.info/?p=129
(I mean, knowledge of all the higher levels)

Sameer: hadd header!

me: Yeah, my friend (and co-blogger at RightShift) is a GIMP freak.
Anyway, my point is that different users prefer to work at different levels.
Some want control and precision,
some just want things to work.
(These sets of people are not mutually exclusive, ob)

Sameer: yup, point taken :)

me: Anyway, getting to the remark I made-
I’ve finally found my comfort zone.
(It’s a zone I didn’t know existed when I was using only Windows)
This zone is the level that provides basic tools for application development; several steps above system calls to kernel networking modules (in the case of Jabber), and two steps below this integrated messaging system in Gmail.

Sameer: hadd hadd

me: That is, the zone where I can create Jabber connections using Python/Perl modules and exert considerable control over how I send messages.
Combined with Bash awesomeness (sed, grep, perl, tr and other text processing tools),

Sameer: yes and the geekiest level will be that of physical layer

me: I can engage in all kinds of crazy-awesomeness.

Sameer: u just flip bits to get your work done

me: Haha

Sameer: http://xkcd.com/378/

me: It would; but I don’t want that kind of control or headache.
I haven’t clicked the link,
and I can already guess that this is the XKCD comic with M-x butterfly. :)

Sameer: hehe, yes… :D

me: M-x butterfly is a classic.
I think someone wrote a mock version of it into Emacs 23.
Anyway-
god, I keep getting distracted.

Sameer: hehe
yes, the best comic strip ever

me: I’m now comfortable in this zone,
and liking the control/automation tradeoff.
But then this Jabber thing comes along… and refuses to work!

Sameer: hehe

me: It’s like being kicked out of a plush leather sofa.
Due to an overabundance of tinkering time,

Sameer: shit, I can relate to that

me: I’ve gotten to the point where I consider most web browsers (save elinks) over-designed,

Sameer: (being kicked from sofa)

me: and most chat clients needlessly bloated.
Basically, I view “software” with a critical eye now that I can (in principle) build my own with a high level language.
And then this Jabber thingy pains me for seven hours and I’m forced to use Firefox.
This is like the ultimate cosmic insult.
And that’s what I wanted to say all along.

Sameer: hadd :P

me: I should make this a blog post.

Sameer: damn, think of how much fun we could have had, if only you had chosen Berkeley :P

me: Fun in our spare time- but would we have had any spare time to begin with?

Sameer: yeah definitely :P

me: You know what, I’m just going to upload this conversation from where I started the levels-of-PC-use rant to the blog.
And I will use… a CLI based blog client to publish the post!

Sameer: hadd :)

me: Can I leave in your name?

Sameer: with my hadd’s :D

Sameer: sure, make me famous :P

me: You’re kidding me.
My blog gets two visitors an year.

Sameer: ah, then allow me to put it as status message :)

me: OK, but think- how many people will have a clue of what’s going on?
The problem with me blogging is that I choose awfully esoteric topics.
And not on purpose!

Sameer: ya…that is the problem with technical writing :(
you have only a given set of audiences

me: We had the exact same problem in the Astronomy club.

Sameer: and only 5-6 people on my gtalk list who will actually read them

me: Hah! I have only 5-6 people on my gtalk list, period.

Sameer: I knew it was coming

me: Ooh, more fun.
I pasted in the chat log with the timestamps.

Sameer: so?

me: This means I now get to use… regular expressions!

Sameer: hehehe
u need help!
hehe
okay, now I need some sleep

me: s/^[0-9:]+//

Sameer: hmphh

me: Goodnight!

*

A chat log, because this kind of special rubbish requires a better resting place than an obscure email account. (The collocutor, Sameer, is like an online punching bag for this kind of outburst.)

Written by Karthik

May 23, 2009 at 9:51 pm

Posted in Rants

Tagged with , ,

Cryptonomicon

with 2 comments

Cryptonomicon is bat-shit crazy.

It’s compressed hyperbole described in such deadpan overtones you have no choice but to run with it. And boy, does it run. It runs a skein across generations and across genres, romping through at least one country every fifteen degrees in latitude, spanning descriptions of hacker culture, the origins and evolution of crypto, fantasy role-playing, the pacific in WW-II (culminating in General Douglas McArthur’s curb-stomping of the Nippons), Tolkienesque metaphors, Haiku, digs at venture-capitalism, mafia machinations, number theory, an irreverent deconstruction of Greek theology, treasure hunts for war gold, and the first ever digital computer with mercury columns in thirty-feet lead pipes serving as RAM.

There’s more of this, but Ares himself couldn’t drag me back to that tome in search of better examples.

If the outrageous awesomeness isn’t getting through to you, consider this: The lead characters are (i) A gung-ho marine in the hobby of reading Japanese poetry who runs halfway across the world engineering fake accidents for the Germans to discover, including ramming a battleship bow-first into the Norweigan ice shelf (ii) A UNIX system administrator who fools an entire battalion of high-tech eavesdroppers by reading text files by flashing the LEDs on his keyboard in Morse, and (iii) A clique of world war II mathematicians headed by Alan Mathison Turing. Yes, Alan Turing is a lead character in Cryptonomicon.

Cryptonomicon isn’t recommended reading. It’s the kind of reading required when you think the world is not crazy-awesome and need someone to bash in your ill-conceived preconceptions with a sledgehammer, which for me is admittedly most of the time.

Written by Karthik

May 6, 2009 at 3:54 pm

Posted in Verbiage

Tagged with ,

On the incorrigible character of Saybolt Universal Seconds (and CLI calculators thereof)

leave a comment »

A considerable chunk of my time as an undergraduate was spent designing hypothetical steam power cycles (entirely on paper, the defining characteristic of an Indian education) and mixing up data on hydrodynamic bearings and lubrication oil viscosities in conveniently provided combinations to yield still more data.

It was as dry then as it sounds now.

The cherry on top of this gargantuan mound of frustration was this: An hour long calculation, meticulously carried out to the right significant figure, an end to a week’s worth of futile drudgery, a manuscript of unbendable proportions and a palimpsest of scrawls, margins, sub-margins, crosses and cross-references that would terminate in a number that would fail a sanity check. Or worse, it would not.

Somewhere in the mess of Kilojoules per kilogram per second per Kelvin and Saybolt Universal Seconds* would nest a skipped exponential, or would manifest a quirk of the almighty tabulators** who would choose, in their earnest and masterful attempts at obfuscation, a system of units that differs from SI by a sliver- enough to evade a perfunctory gaze while puncturing all attempts at veracity.

It Need Not Be So. Alerted via a mention on XKCD, bolstered by the acumen of dozens of dimensionally aware readers***, I discovered that a tool to deal with headaches of the sort exists, has existed for years now, and is available nearly everywhere.

They call it the Google Calculator.

Alas, access to the tool is buried behind web browsers and HTML forms, which some people will attest to be pox-infested heathen land. Of course, necessity is the mother of most scripting (the scant remainder being General Frippery of the Insufferably Needless Kind), and alternatives have surfaced aplenty. (Here are five of them.)

What does it do? Let BASH do the talking:


$ gcalc “1.2 gallons in ml”
1.2 Imperial gallons = 5,455.31025 millilitres


$ gcalc “1.4198 petabyte in MB”
1.4198 petabyte = 1.52449864 x 10E9 megabytes


$ gcalc “131 USD in INR”
131 U.S. dollars = 6,497.69357 Indian rupees

But you knew that.


$ gcalc “234.089 * sqrt(7) * ln(12)”
234.08900 * sqrt(7) * ln(12) = 1 539.00526

Now we’re warming up.


$ gcalc “0.21 tesla in gauss”
0.21 tesla = 2100 gauss


$ gcalc “28 MPa in psi”
28 megapascals = 4,061.05666 pounds per square inch

Still, not a life saver.


$ gcalc “radius of earth * pi * 2 in feet”
radius of Earth * pi * 2 = 131,478,951 feet


$ gcalc “radius of sun^3 * 4 * pi / 3 in km^3″
((radius of the sun^3) * 4 * pi) / 3 = 1.40922394 x 10E18 km^3

We’re getting there!


$ gcalc “12 km/h * 22 minutes in miles”
(12 km/h) * 22 minutes = 2.73403325 miles


$ gcalc “1 / (4 milliohms * 14 microfarads)”
1 / ((4 milliohms) * 14 microfarads) = 17.8571429 megahertz

Wow.


$ gcalc “3.54 kilojoules / (kg * kelvin) * 0.0021 kg / s * 24 kelvin”
((3.54 kilojoules) / (kg * kelvin)) * (0.0021 (kg / s)) * (24 kelvin) = 178.41600 watts

We’ve arrived. Saybolt Universal Seconds didn’t make it. Serves ‘em right. We ought to FITGD, them Saybolt viscometers.

Here’s how you get this running.

My calculation troubles have been deflated, the palimpsests discarded. (Life is good again.)

Google Calculator is amazing. But does it pass the sanity check?


$ gcalc “the answer to life the universe and everything”
the answer to life the universe and everything = 42

Aye, it does.

* An oxymoron of sorts, that.
** These are the Powers That Be, the Catalogue Lords, the spawn of Napier himself- minus his engineering ingenuity at laying waste to bovines from two miles away.
*** This footnote shall not discuss the pun it was referenced by.

Written by Karthik

April 26, 2009 at 6:49 pm

Posted in Discoveries

Tagged with , ,

define:’d

leave a comment »

Several browser cache wipes later, this is all that remains define:’d.

Volume IV

Aboyne (…)

Anthology

Eugenic (It troubles me to see smarter nephews around.)

Exegesis

Gulag

Knaves (surprisingly, phonetically similar to naive)

Obscura (not a word.)

Oleaginous (cognate)

Petulant (campsite, insect bite)

Simulacrum (similar, not)

Cassandra (Aren’t we all?)

Nihil (-ism.)

Scion

Suzerain

Tenebrous (murky. No wit this time.)

Interesting side note: I encountered three of the above words in Anime fansubs. It’s a paradox of sorts- Terrible-to-mediocre translations with surprisingly precise obscure wording. I suppose Japanese is embedded far too deep in the context of its culture to lend itself to meaningful literal or semantic translation.

Written by Karthik

April 23, 2009 at 5:28 pm

Posted in Verbiage

Tagged with

Tooling Around

with 2 comments

It is instructive to compare the amount of time spent learning the tools of a trade to the time spent practising the trade; You’re almost certainly doing something wrong, in the short term, if the former is disproportionately longer than the latter.

Case in point? I spend over two months looking for the perfect blogging client, nitpicking things like keyboard shortcuts (and their absence, thereof), GTK dependency bloat and ‘Movable Type’ support. I don’t even have a movable type blog. The search for the ultimate blogging client has been more-or-less consummated, but at the expense of an indelible fallow period.

Initial Excitement Syndrome

My drive to learn the tools of general PC frippery far outweighs that to engage in the frippery itself- it’s the ultimate malaise. Two years on a Linux box have been witness to five desktop environments, six window managers, five (or more) terminal emulators, two text editors (yeah, Vi and Emacs), three content publishing systems (with little published content)- even three metadata taggers- and while we’re at it, several distros. It’s a wonder I got any work done.

A conservative estimate of the number of application keyboard shortcuts/commands I’ve memorized over the years (excluding Alt-Tabbing and other Windows keys) suggests a figure between six and eight hundred key combinations and commands. If that sounds unreal- here’s the Ratpoison (window manager) cheat sheet. That’s one of several.

The truly egregious excesses, though, have been the misadventures in the murky depth of scripting language hell. What began as a simple mass renaming requirement led to a whirlwind tour of Bash scripting, Sed, Perl, Scheme, Emacs-Lisp, Python, and for reasons baffling in retrospect, GNU-Octave and GNUplot. The Sisyphean task of writing the mass renaming script was carried out over a period of sixteen months, culminating in four lines of code, at the cost of losing the ability to code in one scripting language without involving the syntax of all of the above- a rosy state of affairs.

In the long term, versatility in fooling around might be a good thing- I haven’t been doing this long enough to become blindingly proficient at it- but clearly, taking sixteen months to rename three hundred files rips apart the threadbare argument.

No matter. A near perfect lightweight blogging client is now at hand, and fittingly enough, the inaugural post is a reflection on the tendency to value the tools over the trade.

A resolution of sorts, then. Fewer tools, more work.

Written by Karthik

January 20, 2009 at 11:16 am

Posted in Experiments, Verbiage

Tagged with , ,

define:’d

leave a comment »

Volume III

Volume I, Volume II

define:” searches for the past two months:

Ablative (Ab, from)

Apotheism (A-ha! I’ve been looking for this word)

Bellcross (of the Greek hero cult; despite the name, sadly not a demigod)

Causative (Causative for? Causative of?)

Chortle (Is too close (phonetically) to choke for my liking)

Concubinage (You can figure this one out)

Dongle (D-R-M. Hehe)

Enamored (Yeah. Like Fallout 3 and me)

Facetious

Fandango (Hence the GRIM!)

Heathen (Pagan. I think)

Indelible (Like your presence on the Internet.)

Lentil (I’ve never seen these)

Maw (Maw-th)

Overture

Punitive

Regale

Regent

Rodomontade (Talk about obscurantism…)

Semiotic (Symbol-wise)

Slovenly

Verdant (Don’t say Vista)

Virtuosity (I have no idea)

Wino (It’s an actual word!)

Written by Karthik

November 30, 2008 at 8:08 pm

Posted in Experiments, Verbiage

Tagged with

A regular expression for primeness

leave a comment »

Lazy Saturday afternoons spent on the Internets lead to the niftiest discoveries.

A Regular Expression that tests a number for (un)primeness:

/^1?$|^(11+?)\1+$/

The input string that the above regex is matched with is a sequence of N ones, where N is the number tested for primeness.

The confounding terseness of regular expressions isn’t surprising after an year of daily use, but this one led to record amounts of head-scratching.

Let’s expand that out, shall we?

qr/
^1?$       # 1 or nothing, not prime
|             # OR
^(11+?) # 2 or more 1’s, matched minimally
\1+$       # Followed by one or more instances of captured pattern (pattern in parens above)
/x

When matched against a sequence of N ones (111..), the special cases of  zero or one ones is handled by the first part of the regex; the latter part is the workhorse.

The string is tested against an integer number of repeats of the captured pattern. 111111 is matched with three occurrences of 11- once during the capture and twice during the backreference (\1)+, so 111111 is not prime.

But here’s the trick: Since (11+?) matches minimally, failure to match nine (111111111) as four occurrences of 11 causes the regex engine to backtrack and capture 111; Nine is then matched with three occurrences of 111! So the regular expression engine is trying to match the input with a multiple of a successively increasing sequence of ones, which, of course, is the usual way of testing a prime.

This piece of regex magic was picked off StackOverflow, where there’s a full explanation with examples.

Written by Karthik

November 15, 2008 at 11:41 am

Posted in Discoveries

Tagged with ,

We interrupt the ongoing inactivity…

leave a comment »

…to inform you of the masterpiece that is Mass Effect.

What do you think they're looking at?

It’s better than Galactic Deus Ex.

It’s more than an evolved KOTOR.

It hits harder than Firefly.

It’s what you’ll dream about for days when you’ve played it.

It’s Epic. In a sense that few games are, or can be. It [minor spoiler alert] has an entity dismissively projecting:

We are Infinite. We have no beginning. We have no End.

We are Legion.

Also, it’s taken over my life in the past two weeks; causative for the chain of events that has culminated in weight gain (mass effect!), sleep loss, general neglect and a fallow blog, all to be remedied as befits their cooldown times.

I owe RXFL an apology.

Read the rest of this entry »

Written by Karthik

October 15, 2008 at 10:59 pm

Posted in Personal

Tagged with ,