Wednesday, August 20, 2008

Quine programs

A “quine” (or “selfrep”) is a computer program which prints its own listing.

complex yet trivial code to print the own source file is

#include
char *p="#include%c%cchar *p=%c%s%c; %c%cint main(){%c%cprintf(p,13,10,34,p,34,13,10,13,10,13,10,13,10); %c%cgetch();%c%c }";

int main(){
printf(p,13,10,34,p,34,13,10,13,10,13,10,13,10);
getch();
}


Lot more about this type of programming in
http://www.madore.org/~david/computers/quine.html

Wednesday, June 25, 2008

Googly Google

Well, just like everybody I was there doing my business, yes browsing through technical blogs for passing time. Trust thats one good freetime job. So I came across this blog, it has informations to help you chat well and also to write Project Management Exams.

http://sanjaykattimani.blogspot.com/

From that blog I came across this Google's yet another invention or what ever it is, Toogle. Nice and bubbly.
Just like Google image search, here the slight difference is the image is drawn with the search string.

Speaking of searches and Google, this feature really helps you if the website you are looking for has no custom Google search.

http://www.google.com/advanced_search
And trust me, it gives better result than custom search.

I will sure update more about Google's products.

Intriguing Puzzles

As I am preparing for my placements, I was on the lookout for books with puzzles to work out and to train the mind to think. I came across this book name in many forums I saw. So when I searched for this book, viola I got a pdf copy of the book online. The book name is Shakuntala- Puzzles to puzzle you. This book is recommended for anybody who is looking forward to Infosys interviews.

P.S : solutions are at pg.89

http://www.scribd.com/doc/2052493/Devi-ShakuntalaPuzzles-To-Puzzle-You

Thursday, June 12, 2008

Puzzles

Interesting how these puzzles make your mind fresh. Anyways, I am right now in a puzzle solving spree.. So I was searching for some websites with puzzles .. and found an excellent website, where I can learn and think,

http://en.allexperts.com/q/Puzzle-Solving-1841/

Trust me, this is good. I am going to blog down the supporting theorems and techniques for later reference.

Bayes Theorem:

P(A|B) = P(AnB)/P(B)

P(A|B) - conditional probability. Probability of A happening given B has happened.
P(A) & P(B) - prior probability.

Multiplication tricks:

Multiplication of two digit number by 11:
The result will be the first digit, sum of the two digit and the second digit of the given number.
Eg: 4_2 is the two digit number. The result would be 462, where 6 is sum of first (4) & second(2) digit.
Given 4_8, the result would be 528, since when we add 4+8= 12 add 1 to first digit, and follow the rule above.

Multiplication of any number by 5:
*Divide the number by 2
*If there is a remainder, add 5 to the answer. Else, add 0 to the remainder.
Eg: 246 * 5 :
246/2 = 123 (no remainder)
So result would be 1230.

Say, given 235 * 5:
235/2 = 117(there is a remainder)
so result will be 1175.

Division Techniques:

Divisible by 4, 8,16:
Any number is divisible by 4, only if the last 2 digits of the number are divisible by 4. Similarly for 8 and 16, the last 3 and 4 digits should be considered.
Eg:
5632 is divisible by 4, since 32 is divisible by 4.
It is also divisible by 8, since 632 is divisible by 8.
It is also divisible by 16, since 5632 is divisible by 16.

Divisible by 3,9:
A number is divisible by 3 only if the sum of its digit is divisible by 3. Similarly for 9, the sum of digits should be divisible by 9.
Eg:
23541 is divisible by 3, since 2+3+4+5+1 = 15 is divisible by 3.
334242 is divisible by 9, since 3+3+4+2+4+2=18 is divisible by 9.

Divisible by 11:
A number is divisible by 11, only if the difference between the sum of its digit in the odd places and the sum of its digit in the even places are either 0 or a number divisible by 11.
Eg:
4832718 is divisible by 11. Since (8+7+3+4) - (1+2+8) = 22 - 11 = 11 divisible by 11.



Wednesday, June 11, 2008

To calculate day of the week

When I was reading through some placement question papers, I found this problem asked quite a few times.
Q: Given a date, we have to find out the day of the week.

Solution:
1) C = 2(3-(cent%4))
where cent = first 2 digits of the century
2) y = year + floor(year/4)
where year = last two digits of the year given
3)m = choose from the table















MonthValue
January 0(leap year-3)
Febuary 3(leap year-6)
March 3
April 6
May 1
June 4
July 6
August 2
September 5
October 0
November 3
December 5

4)sum = c + y + m + date given
5)val = sum%7
6) sun - 0,mon-1,tue-2,... sat - 6, use value and find the day

Sample program:
#include

int leapyearMonValue[] = {0,3,3,6,1,4,6,2,5,0,3,5};
int nonleapyearMonValue[] = {3,6,3,6,1,4,6,2,5,0,3,5};

void calculateDay(int date,int mon,int year)
{
int val,cent,monval,yr,i=3,temp,leapyear=0;
int splitcent[4],wrongInput=0;
if(year%4 == 0)
leapyear = 1;
if(mon>12 || mon <1)
wrongInput = 1;
if(date==30 && !(mon%2) || mon==7 || mon==1)
wrongInput = 1;
if(mon==2 && date>28 && !leapyear)
wrongInput = 1;
if(!wrongInput)
{
while(year)
{
splitcent[i--] = year%10;
year = year/10;
}
temp = splitcent[0]*10 + splitcent[1];
cent = 2*(3-(temp%4));
temp = splitcent[2]*10 + splitcent[3];
yr = temp + floor(temp/4);
if(leapyear)
monval = leapyearMonValue[mon-1];
else
monval = nonleapyearMonValue[mon-1];
val = cent + yr + monval + date;
val = val%7;
switch(val)
{
case 0:
{
printf("day is Sunday\n");
break;
}
case 1:
{
printf("day is Monday\n");
break;
}
case 2:
{
printf("day is Tuesday\n");
break;
}
case 3:
{
printf("day is Wednesday\n");
break;
}
case 4:
{
printf("day is Thursday\n");
break;
}
case 5:
{
printf("day is Friday\n");
break;
}
case 6:
{
printf("day is Saturday\n");
break;
}
default:
printf("wrong calculation\n");
}
}
else
printf("wrong Input... check the date, mon \n");
}

int main()
{
int date,mon,year;
printf("Enter the date in format DD/MM/YYYY\t eg: 14/11/2007\n");
scanf("%d/%d/%d",&date,&mon,&year);
calculateDay(date,mon,year);
getch();
return 0;
}


Courtesy:
Wikipedia

Tuesday, May 27, 2008

Basic Unix Commands

Some commands were practiced in the lab. So since this is just for future brush up during interviews it will have only extra features, basic features I have to try and remember.

Very effective:
  • chmod - changes permission for directories/files. eg: chmod ugo+r+w+x filename - gives u-user,g-group,o-others the permission to read(r),write(w) and execute(x). For directory, execute permission is required.
  • ls - list files in the directory. The options most commonly used are -l which is to print file list with permissions, time modified, user details and the other option -a prints all files including the . and .. directories.
  • ff - find files. This command is used to search for a file throughout all available directories.
  • diff - The command which is really unique to unix. This command compares the two files and prints the differences with line numbers.
  • | - pipe. This allows commands to be combined.
Very Basic:
  • cp - copy command. This copies file from one location/one name to other location/another name.
  • mv - move command. This moves/changes file location or name respectively. One had to be careful, 'cause this results in deletion of the original file.
  • cd - change directory. When typed with no argument, it takes to home directory.
  • pwd - current working directory.
  • wc - word count. The order is lines,words and characters.
  • gzip & gunzip - compression/decompression of files.
  • finger - cmd to print details about user. Finger protocol (network).
  • telnet, ftp, rlogin - used for connecting to outer hosts.
  • kill - kills process forcibly
  • ps - lists the process run by you.
  • du - prints disk usuage.

Monday, May 19, 2008

Placement Preparation

Phew!!!! Exams are over, now starts the amazing race towards my own cubicle somewhere in a company. For starters, its placement preparation time. Right now I am enjoying and also studying which is a lethal combination.With this introduction, I am going to blog down all my materials for later references. First day, we are starting with Algorithms.



When we say algorithm, we have tons for everything.So I am going to start learning the basic algorithms first. I am reading from planetmath.org. Its really good. So much technicality and maths are there.

Today(day 1) I read about selection sort,binary search,insertion sort,bubble sort.