Wednesday, June 20, 2012

GVIM BASICS & TIP & TRICKS -2 short tutorial

This article is in continuation from my last article gvim-basics-tip-tricks-1. The best thing with GVIM is that each day you get to learn something new and useful to learn.

So more tips and tricks:
1) * : "*" is used to search word near/under your cursor.

2) %: "%" can be used to look for accompanying bracket. {},[],(). For example if your cursor is on "{" and you press "%" the it will take you to the corresponding "}". It can be very useful when used with "d"(delete) or "y"(yank).

3)"q:"- "q:" is used to view your command history, you can copy paste commands from there or even edit the commands and run the commands directly by selecting them.

4)"q/" - used view previous searches.

5) If you like colors then you can put on the color font by ":syntax on" or just put "syntax on" in your .vimrc file and this action will be taken every time you open a new vim window.

6) w! - sometimes when you try to edit write protected file ":w!" can be useful .

7) :wq! - save and exit write protected file.

I will share some tips and tricks here and keep on writing new articles as I learn more.

Friday, June 15, 2012

GVIM BASICS & TIP & TRICKS -1 short tutorial

As I mentioned in my last post I have been working on GVIM for past few months and each day I am surprised by some new capabilities of the tool. I will share some tips and tricks here and keep on writing new articles as i learn more.

Some basics:

1) Save your file with ":w"
2) Exit with ":q"
3) Start writing/inserting text : "i"
4) copy a line: "y"
5) paste the copied line : "p"
6) Save and exit with ":wq"
7) Undo : "u"
8) Redo: "ctrl+r"
9) Search:  use "?" or "/" followed by string to search use "n" for next match.
10) Replace: use ":%s/str1/str2/g" to replace str1 to str2 globally ":%s/str1/str2/gc" will confirm before replacing.
11) To add "tab" before each line select the lines and then press">".
12) ":g/str/g": This command will delete the lines containing string "str"
13) To display line numbers : ":set nu"
14) To turn of display of line numbers : ":set nu!"
15) ":help" : To open help file.
16) ":set ic" :  after this command all the searches will be case insensitive.
17) ":set noic" : after this command all the searches will be case sensitive.

Tricks:
1) In all the command when you give strings and if your string contains special characters (#$@^&\/) then just add "\" befor the special character.
eg: in order to search for "a/b/c" the command should be "/\/a\/b\/c"

2) you can have a .gvimrc file in your home directory in which you can write all the set commands like ":set nu" and all the other commands listed up. GVIM will read the file every-time it starts and will apply the settings.

3) Type "/" and keep on pressing ctrl+p to find what was your previous searches.

4) If you want to delete next 10 lines, then in normal mode type "10dd", for 5 -"5dd"  so trick is <number>dd, same can be done for yank(copy) "<number>yy"

Saturday, June 9, 2012

HOW "VIM" IS THE KING OF ALL IDE's

I used "source insight" for more than 15 months and I just loved the tool and just cannot imagine writing a code without it but then my life changed a little bit and I have to start working on a red hat machine so no source insight.

At start I found it to be very difficult to survive without it but later on doing some search over Internet I found some tools which can save my life while coding on Linux . Some of those life saver tools were cscope, Ctags, etc but the tool on which all these work the king of all Vi/VIM/GVIM. After 6 months of work on gvim now I no longer miss source insight.

The learning curve on vim is quite steep there are loads of stuff you can do on it which you a cannot think of doing on any other IDE. The Internet is full with the tutorials and tips and tricks for these tools. It just took me few weeks to be comfortable with them and use them flawlessly.

So I think I will write an article on tips and tricks I use on vim a collection of all the tricks I accumulate in last 5-6 months. Will try to do that in coming days keep looking for that in this space.


A word of caution for all those who are looking forward to using these awesome tools they are very addictive. Nowadays even when I try to save my MS word doc I try to do it with ":w"(all those who use vim will get the joke ;) )........


Do leave comments about how you feel about these wonderful tools ... Thanks

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.