Wednesday 27 May 2009

Reverse Start

With more than three years of blogging experience, I can claim to be a veteran blogger. Can I not? That too with three blogs, each in a different language makes me special. Doesn’t it? Perhaps yes, perhaps no.

I have mellowed down, I have fallen into the trap. Worse than all that – I didn’t realise that these did happen to me. The trap is that,  after knowing about my audience, I had started to write in a way that the majority would be pleased. Of course,  I started for myself. I had no special reader in my mind. But slowly that feeling of obligation was building inside me.

Now I want to break all those ties in this blog. Thanks to that unknown blogger who inspired me.

Here  I go. I shall write about the philosophy and similar stuff later on. But for a start, let me take a small problem and solve it in different ways.

Reversing a string: How to solve it (with computer)?

Of course, let’s read it from the end to the start. I shall prefer to do that with C.

void reverseloop(char *s)
{
        char *t; 
        t = s + strlen(s) - 1;
        while (t >= s)
                putchar(*t—);
}

I preferred to use pointers than indices. I was not that much into pointer arithmetic in my earlier days of programming. But now it seems quite handy.

I haven’t done any C programming for ages. So forgive me for the mistakes I make. (One could also see that I haven’t done any checking thing – whether s is a null pointer. I assume that it is a valid string)

Now, that was quite easy. Can we have it a bit recursive thingy? - cooler and smaller and easier to understand.

The reverse of a string is nothing but the first character coming after the reverse of the rest. (Which goes on recursively)

void reverseprint(char *s)
{
        if (*s == '\0')
                return;
        reverseprint(s+1);
        putchar(*s);
}

Isn’t that easy to follow? If s is a null string, just do nothing. Or else, reverse the rest of it. Once the rest of it is reversed, then print the first character.

As a matter of fact, we have written the code to reverse only a null string – which is nothing. The rest follows automatically?

If you think this was tricky, let’s have a look at a bit cooler algorithm. :)

The reverse of a string is nothing but the reverse of the second half of the string followed by the reverse of the first half, Right? Well, this is an extended version of the previous method. I prefer to do that with Python.

def revs(str):
        if len(str) == 1:
                return str
        m = len(str)/2
        return revs(str[m:]) + revs(str[:m])

Now, I assumed that you know C and Python. Python code can actually be optimised to a single line. But let me leave it more readable. The only thing I might need to explain is list slicing. Here str[:m] gives the slice of the string from index zero up to index m.

The funny thing is, I hear that there are many “programmers” who cannot program this thing – not even _any_ of the methods mentioned. I don’t want to believe it. But it could very well be true.

Signing off, Sands.