Archive

Archive for November, 2005

Encarta in your MSN!

November 30, 2005 Leave a comment

Just add encarta@conversagent.com in your MSN contacts. It is a smart computer that will be available to you as a contact all the times and you can have general search on Encarta in your msn window.

Do add this contact to your mail list.

Advertisements
Categories: Computer Science

The 12 Balls/Coins problem with solution.

November 30, 2005 4 comments

Hi,

Any one who may have taken any programming or technical interview he/she may be familiar with the famous 9 balls or 8 coins/balls problem. Which is stated as, you have 8/9 balls and you know that one ball is heavier/lighter, in how many minimum tries the odd one can be found out. Many people are able to solve it in 2 tries when given an analytical weight balance. Recently I tried the tougher version of this problem which is , When you are given 12 balls and you don’t know that if the odd ball is heavy or lighter, now the task is to find out the odd ball in minimum (3) possible tries and also determine if it is heavier or lighter. Believe me if you try it your self its is really time taking and intelligent process. However here I go with my solution

Divide the balls in 3 groups of 4 each. Name each ball 1, 2, 3, … 12.

Now place the groups A (1,2,3,4) in left pan and group B (5,6,7,8) in right pane of analytical balance. There are two possible outcomes

(Try 1) GROUP A and B Balance Out:

The groups of balls balance out. Now it is sure that the odd ball is in group C (9,10,11,12) lighter or heavier we still don’t know. I thing is sure at this stage is that 1,2, …8 labelled balls are ok, this info will we use.
(Try 2) Now take balls 1,2,3 in left pane and take 9,10,11 in right pane. Now possibility is they either balance out or not.
Case 1: if in try 2 groups of balls balance. Then the odd ball is ball number 12.
(Try 3 in Case 1) Weigh Ball 12 against any other ball. If the 12 weighs heavier than the ball 12 is the odd one heavier ball other wise it is the odd lighter ball.
Case 2: if in try 2 groups of ball don’t balance. If right pane containing (9,10,11) is heavier then the odd heavy ball is in this pane, other wise the odd ball is lighter in this case.
(Try 3 in case 2) : if (9,10,11) are heavy then weigh 9 and 10. if they balance out 11 is the odd heavy ball. Other wise which ever remains heavy is the odd heavy ball. if (9,10,11) are lighter then weigh 9 and 10. if they balance out 11 is the odd lighter ball. Other wise which ever remains lighter is the odd lighter ball.
So far I dealt the case in which group A and B balanced out and tries I took are exactly 3. Now lets deal the case if the Group A and B do not balance.

(Try 1) Group A and B do not balance.
If the group A and B donot balance then odd ball is in 1,2,3…,8 and group C (9,10,11,12) is not containing odd ball. This info can be intelligently used.

(Try 2) If the left pane containing (1,2,3,4) is heavy then (1,2,3,4) contains heavy odd ball or (5,6,7,8) contains odd lighter ball. Take (1,2,5) in left pane and (3,6,10) in right pane if they balance out then odd ball is either 4th one (heavy) or 7, 8 (lighter). Now for Try 3 in this case weigh 7 and 8 if they balance 4th is odd heavy ball other wise whichever remains lighter is the odd lighter ball. If (1,2,5) and (3,6,10) do not balance then and (1,2,5) is heavy then odd ball is either (1,2) heavy or the ball 6 (lighter). [Try 3] Weigh (1,2) if they balance 6 is odd lighter ball or whichever remain heavier is heavy odd ball. If(1,2,5) is lighter then 5 is lighter or 3 is heavier. [Try 3] Weigh 5 against ball 10 if they balance then 3 is heavier odd ball else 5 is lighter odd ball.
In Try 2 if left pane(1,2,3,4) is lighter then repeat the process in reverse and you will find out the lighter odd ball in Group A or heavier ball in group B.

Well this solution costed a comprehensive amout of time to think because there was a lot of interruption in office. Any ways some one may come up with better and fast solution if it exists.

Haroon

Yups! The XAML is running against all Odds :)..

November 25, 2005 Leave a comment

Hmm..

Just downloaded the Windows SDK (Microsoft Windows Software Development Kit for the November 2005 WinFX Runtime Components Community Technology Preview (CTP)) for Windows XP and Server 2003 and not for Windows Vista which I was extremely waiting for to run XAML & Avalon apps (still Avalon apps didn’t run and compiled XAML is also not running). Well it was a DVD img file about 1 G.B which costed me a lot of time and disk space to convert to ISO files but it didn't run. Thanks to Brad Abrams that his blogs solved my problem. I mounted that img file to my hard and then ran the SDK and it ran fine. But when I tried to run the XAMLPad.exe it gave me an error that An Un-Handled Exception “System.IO.FileNotFoundException” has occurred in XAMLPad.exe. Though (XamlPad_Saved.xaml) existed. So I then installed the WinFX run time components and finally finally the XAMLPad ran successfully. It's the 3 rd time I have tried the whole process and installed Visual C# 2005 Express Edn on my machine. Eeach time the Win SDK has a conflict with beta of Win SDK for .NET 2.0 or 2.0.50215.4 or runt time components missing. So now I have successfully ran the XAML rendering app.

Well desperately waiting for Visual Studio 2005 (still using express 😦 ), SQL Sever 2005( not using), Team Foundation Server(not using), Avalon(beta), Indigo(beta) , XAML (un-compiled) and what ever is getting my way of approaching Microsoft technology.

Regards,
this->Haroon. My Blogs!

Categories: Computer Science

C Myth !

November 17, 2005 4 comments

Hi Folks!

While in Sofitel, sitting in the luxurious and majestic Halls, having the Microsoft Candies & Coffies, Ramnaresh ( a geek looking Indian from Microsoft Core OS ) was in fornt of me and asked me this thing,

Suppose you have a C struct like this one,

struct Example
{
public:
/* members */
Example * ptrExample;
/* members */
};

While he was writing this to a filling pad I thought Huh so easy struct. He then instantly asked me to write a method as follows,

ADDRESS FindStartAddress(ADDRESS pMAddress)
{
ADDRESS startAddressStruct = null;
/*Code*/
return startAddressStruct;
}

the Parameter to this method ADDRESS pMAddress is the in memory address of any member of this struct. Now the problem was to return the starting address of struct in memory when you only know address of 1 of its members (which member we dont know).

At that time i gave a very ambiguous answer and very briskly too and try to indulge him in some pointer arithmetic, at my answer he said "You answer is ambiguous, and doubtful". Till now I have pondered over this question a lot, and now I think it is not solvable. This was last question of his interview with me, he left and I went to ever pathetic general people!

If any one can solve this I will def. rank him higher in C than me 😉

P.S: Me at Microsoft Interviews and Ramnaresh in Core OS Division, wrote the TCP IP Stack for MS IE.

Regards,
I, Haroon

Unzipping Files from Zip Archive !

November 17, 2005 1 comment

Here comes the code for Unzipping files.

public void UnzipFile(string source, string destdir)
{
// This method unzips all files in "source" directory
// and unzips them in the destdir folder
if( (source.Substring( source.Length – 4 , 4)).Equals(".zip") == false)
return;
ZipInputStream MyZipInputStream; // for reading zip stream
FileStream MyFileStream; // for writing the contents of each file
MyZipInputStream = new ZipInputStream(new FileStream( source , FileMode.Open, FileAccess.Read));
ZipEntry MyZipEntry = MyZipInputStream.GetNextEntry();
while( MyZipEntry != null ) // till there are entries in archive
{
MyFileStream = new FileStream( destdir + "\\" + MyZipEntry.Name, FileMode.OpenOrCreate, FileAccess.Write);
int count;
Byte [] buffer = new Byte[4096];
count = MyZipInputStream.Read(buffer, 0, 4096);
while (count > 0) // this loop copies the contents of archived file in orignal text file
{
MyFileStream.Write(buffer, 0, count);
count = MyZipInputStream.Read(buffer, 0, 4096);
}
MyFileStream.Close();
MyZipEntry = MyZipInputStream.GetNextEntry();
}
MyZipInputStream.Close();
}

Regards,
I, Haroon

Categories: Computer Programming

Zipping and Unzipping files Programmetically.

November 16, 2005 Leave a comment

Following is the C# source code for Zipping a file using SharpZip

public void ZipFile(string source, string dest)
{
// this method zips the file form source directory to destinaton with same name
try
{
ZipEntry entry = new ZipEntry(Path.GetFileName(source));
FileInfo fi = new FileInfo(source);
entry.ExternalFileAttributes = (int)fi.Attributes;
entry.Size = fi.Length;
FileStream input = File.OpenRead(source);
ZipOutputStream output = new ZipOutputStream(File.Create(dest));
output.PutNextEntry(entry);
Byte [] buffer= new Byte[8192];
int len;
do
{
len = input.Read(buffer, 0 , buffer.Length);
if(len > 0)
output.Write(buffer, 0, len);
}while(len!=0);
output.Close();
input.Close();
}
catch
{}
}

I will soon upload the unzip code.

Haroon

Categories: Computer Programming

Try this problem and if you find a solution do ping me!

November 15, 2005 11 comments

Two sets A = {a0,a1,a2,…,an} and B = {b0,b1,b2,…,bn} having integer in increasing order (Sorted arrays of size n). We have to select n largest numbers from the set {ai+bj} ( This set is the sum of two numbers one taken from set A and one taken from set B. The task is to get n largest numbers in O(n). I have tried it many times and found very close solutions that fails on 5%cases. Though at times i think of improving the solution but i am really tired of this. So if any one finds the correct solution with proof do mail me.

Regards,