Pages

Thursday, December 4, 2014

Get next Permutation - C program

Rule:

1. Find largest index i such that array[i − 1] < array[i].
2. Find largest index j such that j ≥ i and array[j] > array[i − 1].
3. Swap array[j] and array[i − 1].
4. Reverse the suffix starting at array[i].

Code:
#include "stdio.h"

#define MAX 100

void ins_sort(int *a, int n) {
    int i, j, tmp;

    for (i = 1; i < n; i++) {
        for (j = i; j > 0; j--) {
            if (a[j] < a[j-1]) {
                tmp = a[j];
                a[j] = a[j-1];
                a[j-1] = tmp;
            }
        }
    }
}

void reverse(int *begin, int *end) {
    int tmp;
    while (begin < end) {
        tmp = *begin;
        *begin = *end;
        *end = tmp;
        begin++;
        end--;
    }
}

int next_permutation(int *begin, int *end)
{
    int tmp;
    int *i, *j, *k;

    if (begin == end)
        return 0;

    /* Single element */
    i = begin;
    ++i;
    if (i == end)
        return 0;

    i = end;
    --i;

    while (1)
    {
        j = i;
        --i;

        if (*i < *j)
        {
            k = end;

            while (!(*i < *--k));

            //swap(i, k);
            tmp = *i;
            *i = *k;
            *k = tmp;

            reverse(j, --end);
            return 1;
        }

        if (i == begin)
        {
            reverse(begin, --end);
            return 0;
        }
    }
}

int main() {
    int n, a[MAX], i;

    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }

    ins_sort(a, n);

    while (next_permutation(a, a+n)) {
        for (i = 0; i < n; i++)
            printf("%d ", a[i]);
        printf("\n");
    }
    return 0;
}


Thursday, September 18, 2014

Breakfast Drive in BMW

It was a Wednesday morning. I saw an advertisement in my Facebook wall from zoomcar.in saying that they were renting BMW 320D discounted on weekdays. It costs 780 rupees for four hours Monday-Thursday. Only 4 hours is the minimum we can book. Immediately I asked my friends in whatsapp “BMW service at your office on just 200 rupees”. Everybody replied with a hand accepting the deal.

I booked the BMW for Monday morning 6 AM to 10 AM. So we can go to office after the drive. The rent has to be paid immediately and security deposit Rs.5000 could be delayed till the day before of the trip. Saturday morning once again I confirmed my friends and paid the security deposit.

Sunday evening we decided the meet-up points and pick-up plans. I woke-up at 4.30 and called Abi. Went to Abi’s PG at 5.30 to pick-up her. We reached Brigade Metropolis at ITPL at 5.45 where the BMW is ready. As I have made the registration, I have to be there in person to do formalities and receive the car. The guy in there was a nice guy. He marked all the scratches, showed the documents and gave the car to us.

It was the moment I lived for. I’m inside a BMW at the wheel. My Adrenaline rushed to my head. I pressed the start button. With small cough the Horse opened its eyes. Abi was next to me in the front seat. Now I realized that it was an automatic transmission and I have never drove an automatic car. I lowered the window and called guy to show me how to put gear! The guy was shocked but anyway he showed the controls.

No clutch so No rev-up. I released the break, and BMW started glide. I was two inches above the seat literally flying in between clouds. The steering was so smooth. I never felt driving a car. It was BMW! I couldn’t believe its happening for some moment.

We parked the car at the entrance of Brigade Metropolis and took some photos. Photos was the only purpose my friends accepted this deal. I called Gyan. They were at Marathahalli at the moment. After 10 minutes they came. In the mean time we took some more photos.

Abi was at the front. Smriti, Dinesh and Gyan were at back seat, Dinesh was in the centre. Fortunately none of them knew driving. So I’m the one to drive the entire 40 Kilometres (Only 40 KM were allowed for four hours). We want some good photos. So I took the car to some rural place where traffic will be less and we could stop the car for pictures. I have chosen the road in the following map.


That was around 16 Kilometres from Brigade Metropolis. We parked the car roadside and took pictures. The place and early morning light was very good for our pictures. We were there for almost an hour taking photos. The people around the place started looking differently. At last we came to an end of our photo session as we all were hungry.


We moved in the same road found a road side idly shop. Stopped a BMW and had breakfast there! Smriti said that she was not that hungry and she need only two idliys. She had four Idlys, one plate rice, three Vadais. This was our second large expense in this trip. The first one was the rent to our car. Dinesh paid for the food. The food there was good (that would be understood by Smriti’s diet). And we all had tea.


Then I taught driving to all. Dinesh was so lucky. He started his driving part of his life in an BMW. Lets his achievements grow. All drive for some 500 metres. Smriti’s drive was the fastest and Abi’s drive was the scariest! Gyan, sorry man. My mind is bad with boys’ memories. Did you actually drive the car?!

We went some far to take an U-turn. We had a railway track in between. The gates were closed. Wow! I stuck in traffic in an BMW. What an experience!



While coming back the road was full. Peak hours in Bangalore. I drove one hour the last 10 Kilometres. But that was actually fun. We lowered all the windows and played “Velai Illa Pattathaari” songs in full volume. The entire traffic had an eye on us. Especially a family with kid in a Wagon-R next to us in a signal. The wife’s stomach burning hotter than the BMW engine. And the day was an unfortunate to her Husband. Hey dude you’ll feel the heat in the night at the bed! :P

Then a girl in an Alto. It was so clear in her eyes. Jealous! Dinesh waved her. After that she never seen her right side. Not even her right mirror. :D At last our excitement came to and end. We dropped the car at 9.55 AM. And my security deposit was safe.

And at the end a selfie to complete the session. Not actually a selfie. A selfoid. Smriti took the photo and Gyan simply leaned his hand to make the photo look like a selfie. ;)


My “Breakfast drive in BMW” album in facebook got 73 likes. https://www.facebook.com/balakumaran.kannan.1/media_set?set=a.514424185360333.1073741827.100003782681809&type=3

Sunday, January 26, 2014

Linux device drivers - 3rd editions - examples

This blog entry may be helpful for people to progress the example device driver mentioned in the book.

The book is available in Internet for free to download.

The code provided here is tested in the machine having
Ubuntu-12.04 running kernel-3.2.0-23-generic.

Here the working code after finishing chapter-3
https://github.com/kumaran127/ldd3/archive/Chapter-3.tar.gz