Friday, May 25, 2012

Petrol Politics

"The Monk who sold his Ferrari knew about the Petrol price hike !!!"

Petrol prices were raised by 7.50 rupees in Delhi and by similar amount all over the country 2 days back since then I have read, heard, talked, debated, joked a lot about this topic so I thought it is going to be a topic I am going to write about.
Petrol as we know now is the deregulated petroleum product in our country which forces many people to simply believe that its not government which is rising the prices but the Oil marketing companies who just want to cover the losses as they are not compensated by government for their losses on Petrol. But this is not the complete truth.
So petrol being considered as rich-man's fuel was deregulated by our government on june 2010, which basically means government will not give subsidy for petrol and therefore have no control over prices and Oil companies will decide the petrol prices. Government did this in order to  bring the fiscal deficit down and improve the overall health of Indian economy.
So starting with some facts:

FACT 1:India imports crude oil from different nation where oil is in abundance. This crude oil goes to refineries where it gets converted to Petrol,diesel and other products, and finally oil mkt. companies provides it to customers. So price of petrol In our country should be driven by crude oil price.. and you will be shocked to know that crude prices has been falling from the last 3 months the net fall has been around 12-13%.

FACT 2:The government which deregulate petrol to control fiscal deficit waves off half a trillion dollars to corporates. The lesser I say about this the better it would be. Read this :  http://www.thehindu.com/opinion/columns/sainath/article3223573.ece

FACT 3: Even if when Petrol is deregulated why is it that oil companies wakes up after 6 months to raise 7.5 RS and not raise Rs 1-2 every month for 6 months . Note that with the deregulation Oil company chiefs can revise the rates every 1st and 16th of every month depending on the international prices. The only reason I can think of is Government intervention but why would government ask oil companies to raise prices after 6 months ...???? answer is simple STATE ELECTIONS ...
http://www.thehindu.com/opinion/editorial/article3453040.ece

I got these facts just by Googling and reading  newspaper so if one decide to do a complete research can find answer to many questions which pops in my mind every now and then :

QUES 1: How much petroleum subsidy contributed to total fiscal deficit pre june 2010 ? If this is not a major contributor to fiscal deficit what are the other sources??

QUES 2: What will be the impact on inflation with this rise ?? epecially food inflation?

QUES 3 : Government deregulated petrol in June 2010 they why did that had no impact on Fiscal deficit infact , Fiscal deficit has always been rising steeply sice last year .. why ??

TGIPL 6 solution for all three levels:3 Expert

So below is my submission for TGIPL6 expert level question link :


#include<stdio.h>
#include<string.h>

//Assume following return types while writing the code for this question.
int output1[100];
int r,c,done=0,temp_op=-999,num=0;
struct s
{
char a;
int b,d;
}mat[100][100];
void magic()
{
int temp=0,i,j;num++;
if(done ==1)return;
for(i=0;i<r;i++)
    for(j=0;j<c;j++)
        {
        if(mat[i][j].a=='S'&&mat[i][j].d==0)
            {
            if(i+1<r)
                {
                if(mat[i+1][j].a=='P'&&mat[i+1][j].b==0){
                    mat[i+1][j].b=1;
                    temp=1;
                    }
                }
            if(j+1<c)
                {
                if(mat[i][j+1].a=='P'&&mat[i][j+1].b==0){
                    mat[i][j+1].b=1;
                    temp=1;
                    }
                }
            if(i-1>=0)
                {
                if(mat[i-1][j].a=='P'&&mat[i-1][j].b==0){
                    mat[i-1][j].b=1;
                    temp=1;
                    }
                }
            if(j-1>=0)
                {
                if(mat[i][j-1].a=='P'&&mat[i][j-1].b==0){
                    mat[i][j-1].b=1;
                    temp=1;
                    }
           
                }mat[i][j].d=1;
            }
        else if((mat[i][j].a=='P'||mat[i][j].a=='p')&&mat[i][j].b>=1&&mat[i][j].d==0)
            {
            if(i+1<r)
                {
                if(mat[i+1][j].a=='P'&&(mat[i+1][j].b==0||mat[i+1][j].b>mat[i][j].b+1)){
                    mat[i+1][j].b=mat[i][j].b+1;
                    temp=1;
                    }
                else if(mat[i+1][j].a=='E'&&(mat[i+1][j].b==0||mat[i+1][j].b>mat[i][j].b+1)){
                    mat[i+1][j].b=mat[i][j].b+1;
                    temp=1;
                    done=1;
                    temp_op=mat[i][j].b;
                    return;
                    }
                }
            if(j+1<c)
                {
                if(mat[i][j+1].a=='P'&&mat[i][j+1].b==0){
                    mat[i][j+1].b=mat[i][j].b+1;
                    temp=1;
                    }
                else if(mat[i][j+1].a=='E'&&mat[i][j+1].b==0){
                    mat[i][j+1].b=mat[i][j].b+1;
                    temp=1;
                    done=1;
                    temp_op=mat[i][j].b;
                    return;
                    }
                }
            if(i-1>=0)
                {
                if(mat[i-1][j].a=='P'&&mat[i-1][j].b==0){
                    mat[i-1][j].b=mat[i][j].b+1;
                    temp=1;
                    }
                else if(mat[i-1][j].a=='E'&&mat[i-1][j].b==0){
                    mat[i-1][j].b=mat[i][j].b+1;
                    temp=1;
                    done=1;
                    temp_op=mat[i][j].b;
                    return;
                    }
                }
            if(j-1>=0)
                {
                if(mat[i][j-1].a=='P'&&mat[i][j-1].b==0){
                    mat[i][j-1].b=mat[i][j].b+1;
                    temp=1;
                    }
                else if(mat[i][j-1].a=='E'&&(mat[i][j-1].b==0)){
                    mat[i][j-1].b=mat[i][j].b+1;
                    temp=1;
                    done=1;
                    temp_op=mat[i][j].b;
                    return;
                    }
                }mat[i][j].d=1;
            }
        }
if(temp)
    magic();
}
void Escape_Maze(int input1,char* input2)
{
int i,j=1,k,l,cs,rs,ce,re,sum,ns=0,ne=0,nw=0,np=0;
//char mat[51][51];
char *p,*p1;
for(i=0;i<input1;i++)
{
if(i==0)
{
p=strtok(input2," ,{};:.()\"[]-!@#$%^&*_=+|\\'<>?`~");
p1=strtok(NULL," ,{};:.()\"[]-!@#$%^&*_=+|\\'<>?`~");
}
else
{
    p=strtok(NULL," ,{};:.()\"[]-!@#$%^&*_=+|\\'<>?`~");
    p1=strtok(NULL," ,{};:.()\"[]-!@#$%^&*_=+|\\'<>?`~");

}

sscanf(p,"%d",&r);
sscanf(p1,"%d",&c);

for(k=0;k<r;k++){
    for(l=0;l<c;l++)
        {
        p=strtok(NULL," ,{};:.()\"[]-!@#$%^&*_=+|\\'<>?`~");
        mat[k][l].a=p[0];
        mat[k][l].b=0;
        mat[k][l].d=0;
        if(p[0]=='S'||p[0]=='s')
            {ns++;
        mat[k][l].a='S';
        if(k!=0&&l!=0&&k!=r-1&&l!=c-1)
            ns=10;
            }
        else if(p[0]=='E'||p[0]=='e'){
            ne++;
            mat[k][l].a='E';
            if(k!=0&&l!=0&&k!=r-1&&l!=c-1)
                ne=10;
            }
        else if(p[0]=='P'||p[0]=='p')
            {np++;
        mat[k][l].a='P';
            }
        else if(p[0]=='W')
            nw++;
        else
            mat[k][l].a='P';
        }
}
if(ns==1&&ne==1&&input1<=10&&r<=50&&c<=50&&np>0){
magic();
output1[i]=temp_op;
}
else
    output1[i]=-999;
done =0;

   
ns=ne=nw=0;
temp_op=-999;num=0;
}
    //Write code here
}


TGIPL 6 solution for all three levels:2 Medium

So below is my submission for TGIPL6 Medium level question link :


#include<stdio.h>
#include<string.h>

//Assume following return types while writing the code for this question.
double output1;

double magic(int a)
{
int b=2,c=3,temp;
int i;
if(a==1)return 2;
if(a==2)return 3;
for(i=2;i<a;i++)
{
temp =c;
c=b+c;
b=temp;
}
return c;
}
void GetPossibleAssignments(int input1)
{
int t=magic(input1);
output1=t*t;   
if(input1<=0||input1>=1000)
output1=0;
//Write code here
}

TGIPL 6 solution for all three levels:1 Easy

So below is my submission for TGIPL6 easy level question link :

#include <stdio.h>
#include <string.h>
char* output1[100];
int if_swap(char *a,char *b)
{
    int i,j,a1=-1,b1=-1;
   
    for(i=0;a[i];i++)
        if(a[i]>=65 && a[i]<=90){a1=i;
            break;}
           
    for(j=0;b[j];j++)
        if(b[j]>=65 && b[j]<=90){b1=j;
            break;}
           
        if(a1==b1){
            if(strcmp(a,b)<0)return 1;else return 0;}
        if (((b1>a1)&&a1>-1)||b1==-1){
            return 1;}
        return 0;

}
void GetTeamOrder(int input1,char* input2[])
{
int i,j,k=1;
char *temp;
for(i=0;i<input1;i++)
    for(j=i+1;j<input1;j++)
        {
        if(!if_swap(input2[i],input2[j]))
            {
            temp= input2[i];
            input2[i]=input2[j];
            input2[j]=temp;
            }
        }
    for(i=0;i<input1;i++)
    output1[i] = input2[i];
}

Monday, May 7, 2012

Replacement for source insight in Linux

Few days back I changed my job. In my last job I used to work on windows machine and used source insight as my default IDE. In my new job I work on Red hat machine so have no "source insight". First few days I spent on google to find a replacement . So I think it's a good idea to share all the information I got....

1) cscope : a nice tool for exploring large codes ... You just need to save all the files in the project to the file "cscope.files" ... To do this run the following command at the to directory of your code base :
"find ./ -name "*.c">cscope.files"
"find ./ -name "*.h">>cscope.files"
....
And similar command for all the file extension you have in your project .

Finally type command "cscope" and you are ready to go ... It will show you options like find C symbol ,global definition ,find string,replace string, ... And a few more .. Although it is not a complete replacement for "source insight" but you get used to it in long run ..

2) Ctags/tags : this option can be used with vim just prepare the tags file and there you go now of you want to jump to definition of any function/variable/constant/structure .. Just keep cursor over the function and press cntrl+t .. Press cntrl+] to return .. If you are using GVIM then its even more simple   . press cntrl+left-click to go to tag press cntrl+right-click to return.

3) use Eclipse ... Although I have not personally used it but have heard that they have a package for Linux also ..

4) finally if you know the code well and do not need frequent searching .. Then you can survive with emacs/vi/vim/gvim. Actually these are one of the best IDE's if you can master them. Learning curve for vim is quite steep and same goes with emacs. All i can say is that these are very powerful tools only in the right hands. There are a loads of stuff you can do with these tools quite easily which you cannot think of doing with other IDE's.


If you are working on Linux and use different IDE for code browsing ..comments are most welcome

Sunday, May 6, 2012

Coding contests

I keep on participating in coding contests , Its a new sought of hobby which i have these days .. from now on i will post my codes for every contest i participate in .. i will also post solutions to some problems i have solved in past :)

I have participated in following contests:
1) Google code Jam
2) Techgig The Great Indian Programming League.
3) Code chef
4) Top coder
5) interview street
6) Hackerrank

finding text in files linux

Using grep for finding some text in group of files can be sometimes complex specially when u have a directory with multiple hierarchy .. currently while looking for some simple option i found this command :
"find . |xargs grep "your_txt""
if you want to find the text in only .C files then also its simple use command:


"find ./ -name "*.C" |xargs grep "your_txt""

such use of find and grep together with xargs can make life simple many times.