Just another tech blog.

A blog abt GNU/Linux, programming, hacking, and my life.

Archive for June, 2006

Installing color schemes for Vim editor

Posted by ajay on June 24, 2006

If you are busy programming all the day and getting bored with the default color scheme of vim editor. You can install some color schemes for vim editor as you want and then you can switch between them.

for installing color schemes first you need to create two directories in your home directories [if these directories dont already exist]

[user@localhost.localadmin]mkdir ~/.vim [enter]

[user@localhost.localadmin]mkdir ~/.vim/colors [enter]

Thats it. Your pc configuration part is done.  Now you will need the theme configuration files. You can download the latest color schemes and many old ones from here (official website of vim editor).  There are many available themes to suit your mind and mood.

Now for each theme or I better call it a color scheme you will get a .vim file or an zipped archive which after extracting will produce lots of .vim files

now you have to copy all the files to the directory .vim/colors/ in your home directory.

For example, if you have downloaded a theme called matrix.vim, then –

[user@localhost.localadmin]mv matrix.vim ~/.vim/colors [enter]

Bingo, the colorscheme is installed, now whenever you are editing a file, you can change the colorscheme using

:colorscheme matrix.vim

You can install as many themes as you like by copying files into that directory.

Now if you want to make a theme your default theme, then you should add this command to your .vimrc –

colorscheme matrix

Here matrix is the name of the colorscheme.

You can also design a theme for vim editor yourself if you are interested. You can take a look at any existing colorscheme file and change the colors in the file to create a new scheme for yourself.

Any better ideas are always welcome. Happy Vimming.

Advertisements

Posted in vim | 33 Comments »

Programming freaks @ ug1

Posted by ajay on June 22, 2006

While doing C programming assignments .. We all used to CURSE the prof and all .. but after SMR and Uday Vishesh .delivered a lecture about topcoder and all .. many people were programming out of interest not due to enforcement by the prof .. first we people started on topcoder .. but after that started on ACM Valladoid Online Judge and Sphere Online Judge as well . Plus we started taking part on many online programming contests as well like bitwise, bytecode, ICPC, Google India Code Jam etc ..

some of the people from ug1 who are coding are

1. ACM Valladoid Online Judge ( acm.uva .. )

Name No. of Solved Problems Rank ( India ) Rank ( world )
Ajay Somani 108 39 2261
Kulbir Saini 38 85 5216
Sharatchandra Pinapati 13 10630
Atul Kumar Dwivedi 10 12313
Maruti Borker 06 15875

2. Sphere Online Judge ( SPOJ )

Name No. of Solved Problems Points World Rank
Ajay Somani 38 13.2 229
Abhilash I 32 11.8 244
Maruti Borker 23 6.8 354
Sharatchandra Pinapati 19 6.5 362
Sunil Soni 13 3.2 560
Kulbir Saini 7 .9 1064

3. Topcoder

Name No. of Competetions Rating School Rank Country Rank
Ajay Somani 1 1571 1 18
Shikha Aggrawal 2 931 381
Maruti Borker 3 632 23 827
Kulbir Saini 3 602 24 861
Atul Kumar Dwivedi 3 590 25 875
Vivek Prakash —- —–

If anybody from our batch if programming on these sites and his name is left out due to my mistake .. Plzz let me know about it

and Thanks to

1. SMR, Uday Vishesh, Vardhman and Manikandan sir for their motivation.

2. Turbo for his classical effort for this ranklist of top 100 Indian coders at ACM UVA.

Posted in Programming | 4 Comments »

SPOJ Methods to solve for beginners – part 1

Posted by ajay on June 4, 2006

From last 20-25 days I am solving problems on Sphere Online Judge . This site contains the best problemset I’ve seen (even better than acm) It contains a variety of problems and I thought to write my algorithms here so that some beginners like me can get help and improve their programming skills. So here is the first part containing some easy problems –

Disclaimer : I do not claim that my solutions are best in any means, as far as methods are concerned I always prefer simplicity rather then speed, and my methods are just a starting point for beginners. If you have better solutions please be generous to put them as comments along with the problem name.

so here we go –

1. Prime Generater – This question can be solved by applying sieve’s algorithm. What we do is make an array of prime numbers in the range of 1 to 32000 ( square root of 1000000000 ) .. now we have our initial setup ready .. for generating primes i used sieve’s algorithm which states that you make an boolean array of 32000 values and initialize it as true that means declare all of them as prime .. now start from 2 and if the current number is prime .. make all itz multiple in the apropriate range as composite ( i.e. set their index in the array as false .. ) .. this is the fastest method for generating prime numbers .. now we start taking the input from the user .. lets say he gives the numbers m and n as input ( m> n) now we apply sieve again .. first make an array of booleans of size m-n ….. now for all the prime numbers from 2 to the prime number next to the square root of m we make their multiples in the range n to m as composite ( i.e. set their array as false ) .. now after this procedure is over .. just a linear traversal in the second array .. and print whatever is prime .. ( i.e. flag is true .. ) . here we consider primes only till the square root of bigger number beacause of the fact that if any number is divisible by a number .. then either the diviser of x/y( x is the number and y is the diviser ) will b in the range of square root of x .. so it speeds up the simulation a lot .. 🙂

2. Transform the expression – This problem is very easy except you got check all the minor errors .. you can do it easily by a use of stack .. ( that is the method i was taught in my data structure course .. :p .. ) what you do is make a stack of operations .. ( char ) and a stack of operators ( char again .. ) .. now whenever an operator is pushed on to the stack if the the operator at the top of the stack has a priority higher than the operator to be pushed ( for example ‘+’ is to be pushed and ‘*’ is at the top of the stack .. ) we first pop out the higher

priority operator .. apply the operation on the char operands .. print the result and then push the current operator .. I am just telling the basic thought .. now you have to put a lot of conditions to get it working .. and ofcourse the special case for brackets .. because they are not the operators but have got the highest priority .. ) .. this algorithm is pretty much O(n) so will work fine ..

3. The next palindrome .. :- this method I used to solve the problem is pretty much a non standard method .. I mean just specific to the problem .. there is no specific theorum to proove that the method is correct or not .. anyways .. what we do is .. we have a boolean variable isincreased ( which at any time specifies that after all the manipulations we have done till then is the resultant number greater than the input number or not .. ) now make it initially as false then first we travel starting from the starting digit and the ending digit .. ( make one pointer as low and other one as high .. based on your choice .. ) now we keep moving both the pointers towards the center of the number untill high<low comes .. once it comes we make high equal to low and make the isincreased flag to true .. if it does not than we will stop our traversal once we reach the center .. now we will move outside i mean towards the corners of the number .. we again have a high .. ( pointer on the right side .. ) .. and low so we can have 3 conditions at a time ..

1.high < low .. => we make high equal to low and forget about it ..

2. high=low => we do nothing and keep moving

3. low<=high => we increase digit low by 1 and and then make high equal to low ( that means if we have low = 1 and high = 8 we make low as 2 and high as 2 ( ofcourse we have to make them equal at any stage because the resultant number is a palindrome .. ) . and then make all of the digits between low and high as 0 ( becauze since low is increased so even we make all of them as 0 the isincreased flag will still be fine .. )

now the special case is if we have the digits as 9 and their is no chance of increasing the digits .. frm the beginning ( the number is all 9 ) the isincreased flag continues to be false and finally if that is the case when we finish it up .. we simply print 1 than some 0’s then again 1 .. ) ..

otherwise after we reach the ends we are done and we simply print the string .. 🙂

4. Simple Arithmatics .. – this is question is as simple as it looks except the output formatting .. you will get it right but the only statement to be given attention is that horizontal line of hyphens is only as long so as to cover the line exactly above and below it .. and not all the output .. so if u take care about this you may get it right .. that was the mistake i did when i solved .. :p ..

5. Small Factorial .. – This is one of the easiest problems at spoj .. you just have to make a programme to multiply arbitrary large numbers . ( stored as strings .. ) and then run a loop from 1 to n to calculate the answer .. thats it .. at least that is what i did .. ( thr may b better methods ofcourse .. )

6. Bytlandian cryptographer .. act 2nd .. – This is again a very easy problem .. add 1 to a number isnt it easy !!!! .. but the problem is you have to write the code in Brainf*** yeah that language does not have even a multiplication and addition operators .. just 8 instructions and the language is over .. !!! 😦 .. but you can try the official documention of brainf*** language .. go through may be first page and thats it for this problem .. ( I used a trick to get around the solution .. which makes it almost 16 character programme .. !! .. ) ..so easy points once you go through this page ..

Posted in Programming | 29 Comments »

Vim – applying separate settings for different file types

Posted by ajay on June 2, 2006

If you are using vim quite often, you might have configured your vimrc very well. But these settings grow continuously; so it is always not feasable to apply same settings for each and every filetype .. you might want to apply some x settings if you are writing a cpp programme and some y settings when you are editing an html code and so on.  Your vimrc might be growing too large so you might want to separate it into different files; I mean different configuration for different file types.  Here’s what you can do –

[user@localhost.localadmin]vim ~/.vimrc

The syntex for specific filetype settings is

autocmd   FileType cpp source ~/.vim/cpp.vim

autocmd   FileType py source ~/.vim/py.vim

and so on…

now the third word in these lines specifies the extention and the fifth word specifies the file code to be executed .. that means from now onwards if a person will open a .cpp file ( cpp programme ) the vim settings writtin in ~/.vim/cpp.vim .. will be applied ..

you can specify settings for as many filetypes as you want .. and b sure that if you specify a source file in .vimrc of yours then even if you dont want to apply any settings as of now .. plzz create an appropriate file at an appropriate place which you have specified in the .vimrc otherwise whenever you will try to edit that filetype it will give errors .. thats it so now you can apply different settings for different codes.

I will put my .vimrc and some separate settings for different filetypes may be in a day or two .. any comments if you have any better ideas ..

Posted in vim | 55 Comments »

Size contest

Posted by ajay on June 1, 2006

Recently I was solving problems on Shpere online Judge and I found an interesting challange Size contest. The problems statement is that you are given a number n and a set of n integars, you have to calculate the sum of all the positive integars amongst those numbers. But the requirement is not to write the fast program but to write the shortest code for it. You might choose any language. Since I know only c/c++ and python so here is what i wrote in both of them respectively ..

# c++ code ..

#include<iostream>
main(){

int n,s=0,x;

for(std::cin>>n;n–;s+=x>0?x:0)

std::cin>>x;

std::cout<<s;

}

this code is 93  characters without spaces and newlines ..

now here is the python code ….

# python code ..

n=input()
y=0
for i in range(n):
z=input()
if z>0:
y=y+z
print y

this is 53 characters without spaces and new lines ..

can anybody help  me reduce it to 70 ( in c/c++) and 26( in python ) .. which are the best codes for respective languages there or if not to that level at least reduce the code size .. 🙂 ..

Posted in Programming | 10 Comments »

Totem – the complete media player

Posted by ajay on June 1, 2006

Many of the Linux distro’s ship in totem as a movie player with them. Its there in Applications=>Sound and Media=>Movie Player. But due to some copyright problems they dont provide support for most of the file formats, not even mp3. They do provide support for flac and ogg vorbis formats but no body listens to them as they are not common. So it happens that this dumb player seems to play nothing on the system. I recently figured out that we can configure it to play any file format except rm and rmvb (I dont know the plugins to play rm and rmvb but they also can be played) . You just have to provide totem back-end in terms of codecs for different formats. You have two options to do the same – First and the easy one is Xine-lib, and the second one is gstreamer. Xine-lib seems to have some problem so that the player crashes some times and besides gstreamer has other benifits so I go for the second option.

You need the rpms regarding gstreamer. Here are the links for Fedora Core 5 on i386 architecture  (the one which I have) –
gstreamer08-0.8.12-4.fc5.i386.rpm
gstreamer08-ffmpeg-0.8.7-1.fc5.i386.rpm
gstreamer08-monkeysaudio-0.8.2-1.fc5.i386.rpm
gstreamer08-plugins-extra-audio-0.8.8-3.fc5.i386.rpm
gstreamer08-plugins-extra-dvd-0.8.8-3.fc5.i386.rpm
gstreamer08-plugins-extra-video-0.8.8-3.fc5.i386.rpm
gstreamer-plugins-bad-0.10.1-2.fc5.i386.rpm
gstreamer-plugins-ugly-0.10.3-1.fc5.i386.rpm


or if you are using any other linux or any other architecture you can go to freshrpms or might try googling or another option (will work only if you have yum configured properly)

[user@localhost.localadmin]yum -y install gstreamer* [enter] ..

And it will do the rest for you.

If you downloaded the rpms with previous method, you will have to install them using

[user@localhost.localadmin]rpm -ivh gstreamer* [enter]

Thats it. Now your totem player is ready to play anything.

Posted in Linux | 5 Comments »

Taking backup of files using Vim

Posted by ajay on June 1, 2006

Many people have the habit of saving the file after every around 5 minutes .. there might be cases when you want to change something in your programme and see the output .. and if the output is not worth .. you might want to get the original programme back .. or by mistake you delete some lines frm a file and save the file In both cases you need the original file back .. To escape from such situation .. you might move the file to another file each time you want to edit the file but that is very tedious work .. so here is another method .. set the backup option in vim and it will do the rest for you ..

wat you have to do is ..

[x@localhost.localadmin]vim ~/.vimrc

now append these lines ..

set bk

set bdir=~/backup

set bex=bkp

now save the file .. in these lines .. first lines specifies that the user wants to take backup .. ( enables the backup option .. ) . The second line specifies the directory in which the backup will be stored .. the line specifies the string to be appended to the file name to get the name of the backup file .. like you save a file named a.cpp then a file named a.cppbkp will be created in ~/backup directory .. you can skip the third line .. in that case the default string which will be backup files contain is ~ .. ( the example file be stored as a.cpp~ ) ..

From now onwards whenever you save any file its backup will be saved in ~/backup directory .. ( you might choose a different one based on your choice .. ) .. and remember before opening any file CREATE a DIRECTORY NAMED BACKUP in your HOME DIRECTORY .. and the backup system is ready .. and whenever you save a file second time .. the original backup file is rewritten .. I still dont the know the methods for multilevel backup .. if you know .. please let me know by leaving a comment ..

Posted in vim | 2 Comments »

Calculater in Vim

Posted by ajay on June 1, 2006

Sometimes you write complicated mathematical expression in a vim and you need the result immediately .. and you dont want to open any additional application .. here is a method to configure a calculater for vim ..

[x@localhost.localadmin]vim ~/.vimrc

now after opening the vimrc .. append the following line to it ..

inoremap <C-B> <C-O>yiW<End>=<C-R>=<C-R>0<CR>

What this line exactly doing is setting ctrl+b as a shortcut for a complicated method which u have to do to otherwise to calculate the expression .. This calculater is configured for insert mode in vim ..

after this whenever you open a file .. and you write expression like ..

2+2

now press ctrl+b …. and it will replace it with ..

2+2=4

Isnt it funny .. this small calculater takes care of

+ => addition

– => subtraction

* => multiplication

/ => division

% => remainder operator ..

and it also takes care of operater precedence ..

If you have any better ideas .. you might post them as a comment ..

Posted in vim | 4 Comments »

Login in ssh without a password

Posted by ajay on June 1, 2006

Many of you guys have different accounts on different systems on linux .. and you might log on from one system to other system regularly . like me. But each time you login it asks for a password and some of you might not want this as you have to type the password each and every time and this is unnecessary effort . So here is a method you can use for login in ssh shells without a password .

Suppose you want to login from system A to system B on user X. Then you might try these commands .. But you need your password of user X at system B once .. if you have it then type the following commands on system A ..

[user@A]ssh-keygen -t rsa [enter]

RSA here is the type of encryption used .. you can have RSA as well as DSA on most standard unix systems.

it will give the following output

Generating public/private rsa key pair.

Enter file in which to save the key (/home/ajay/.ssh/id_rsa): [enter]

Enter passphrase (empty for no passphrase):[enter]

Enter same passphrase again:[enter]

Your identification has been saved in id_rsa.

our public key has been saved in id_rsa.pub.

The key fingerprint is:

ef:b8:fd:d8:de:63:d2:f0:ea:3b:ba:1b:ad:e9:b6:ce user@localhost.localdomain

[user@A]

dont worry about it now run the command

[user@A]scp ~/.ssh/id_rsa.pub X@B:.ssh/authorized_keys2 [enter]

X@B's password: ******* [enter]

give it the password ..

you are done .. now each time you login frm system A to system B using user X it wont ask you for password .. But be careful as someone else might login to ur private account on B once it gets access to A ..Nyways yeah it saves a lot of work specially for those who keep long and strong passwords for their accounts ..

Posted in Linux | Leave a Comment »

Problems in file transfer through scp

Posted by ajay on June 1, 2006

Well many of u guys use scp for transferring ur files over network.The problem with this is some times when you try to scp over some systems nothing transfers and some crap thing is outputed .The reason of this is that scp is done in 2 parts .Suppose you want to transfer a file named X from machine B to a machine A and you are sitting on machine B. In first part the programmes  log on to the remote machine ( A in this case )  and in second part the files are transfered from machine B to machine A. But when you log on to any system ( A in this case )  the .bashrc or .bash_profile placed in the home directory of that user gets executed and if there are any printing statements in the like echo or nfrm or somethings like that then the second step does not gets exetuted and this command will only dump whatever echo statements are written in the .bashrc of the remote user on machine A. So if you are having the same types of problem with using scp you might try removing all the print statements from the .bashrc of the remote user on machine A. This might work in some cases.

Posted in Linux | Leave a Comment »