Monday, August 31, 2009

Palindrome Test

The palindrome test is very common in interviews. Here is a program that shows how to find out if a string is a palindrome.

Please note that this a simple program as in an interview not much more is expected from you. The test is not meant to commercialize your greatest palindrome rendition. We will not be buying palindrome testing software in the near future. The point of this test is to know your basic knowledge of string manipulation and algorithm generation. This program takes a string and tests whether the string is a palindrome or not. The palindrome can be one word long, or a whole sentence, and can have upper and lower case letters. Note that palindromes are not case sensitive. Also, the palindrome should ignore punctuation marks.

Please note that in my simple algorithm I am assuming that regular ASCII characters are used.

Algorithm:

1) Get string and lets call it testItem;
2) Strip all the punctuation marks, spaces, and symbols,
and store in testItemClean.
3) Reverse the testItem and store it into reverse.
4) Compare testItemClean and reverse.

To find more about palindromes please visit here .


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


int main()
{
char testItem[] = "Was it Eliot's toilet, I saw?\0";
int i = 0;
int currentPointer = 0;
int cPointerClean = 0;
int length = (strlen(testItem))-1;
char *reverse;
char *testItemClean;

reverse = (char*) malloc (length*sizeof(char));
testItemClean = (char*) malloc (length*sizeof(char));

for(i=0; i<=length; i++)
{
/*Reversing testItem and storing stripped
off symbols into *reverse*/

if((testItem[length-i]>=65 && testItem[length-i]<=90) \
||((testItem[length-i]>=97 && testItem[length-i]<=122)))
{
if(testItem[length-i]>65 && testItem[length-i]<90)
testItem[length-i] = testItem[length-i] + 32;
reverse[currentPointer] = testItem[length-i];
currentPointer++;
}

/*Stripping testItem off symbols and storing into
testItemClean*/

if((testItem[i]>=65 && testItem[i]<=90) \
||((testItem[i]>=97 && testItem[i]<=122)))
{
if(testItem[i]>65 && testItem[i]<90)
testItem[i] = testItem[i] + 32;
testItemClean[cPointerClean] = testItem[i];
cPointerClean++;
}

}
if(!strcmp(reverse, testItemClean))
printf("It's a palindrome");
getchar();
}

Bit counting (how many 1's in 5 [binary])

This code is simple and can be enhanced to work in more general cases. If you do end up changing any of the code and wish to post it, please feel free to do, but, do post a link back to this site.

Since so many of you have had problems in interviews regarding bit counting, I have decided to post this program. It counts the amount of 1's that there are in any 32 bit number.



#include <stdio.h>

#define BIT_MASK 0x1

int main()
{
int number = 52;
int i = 0;
int counter=0;
/*Looping for each bit in an int.*/
for(i = 0; i < 32; i++)
{
/*
RF1 -> Shift to the right by 1
52 is 110100 in binary.
Bitwise AND 0 with 1 ---> 0 -> RF1
0 with 1 ---> 0 -> RF1
1 with 1 ---> 1 increase counter -> RF1
0 with 1 ---> 0 -> RF1
1 with 1 ---> 1 increase counter -> RF1
1 with 1 ---> 1 increase counter -> RF1
...
It will continue 32 times except that
in this case (52) the rest will be 0s.
*/
if(number & BIT_MASK)
counter++;
number = number >> 1;
}


printf("The number of 1's in %d is: %d", number, counter);
/* Visual Studio will close the output window very fast.
This trick will keep it open.
*/
getchar();

}

Saturday, August 8, 2009

In Penang - Malaysia

So I have been in Penang - Malaysia for the last 3 weeks. I still have one more week to go. I am here on a business trip doing some embedded software development with Intel. And that's the most I am going to say about that.

I have to tell you, I am very impressed with Penang. When I arrived three weeks ago I wasn't sure about what to expect, or better yet, I expected different things. I had just returned from a business trip to Shanghai and while I loved the city and its people, I had some trouble getting used to the food. Being from Canada I am used to a different type of Chinese food. I arrived in Shanghai a month ago looking for chicken balls and sweet and sour chicken and instead found myself ordering from menus written completely in Chinese and having to pick based on pictures; and these sometimes deceive you. I learned towards the end of my trip that the taste of food I am more accustomed to is the Hong Konk style. So, while I was impressed with the explosion of modernism in high-rise, neon light, big screen Shanghai, and very thankful to the hospitality of its people, the trip left me worried about what to expect in Malaysia.

As soon as I landed in Penang I began to notice that most signs were written in English. Then, when I arrived at The Equatorial Hotel the whole 20 hours travel time began to look like it all might have just been worth it. But the Equatorial will be a subject of another post. This is just a look over Penang. I am very glad to say that most here speaks English very well. It is very easy to get around and I need not worry about getting lost. If I do, I get a cab and the driver will understand what I am saying. And the food...The food, as Robert from Everybody love's Raymond might say, "the food is unbelievable." The melting pot of cultures in Penang makes for an excellent dish. The most common restaurants are what the locals call Hawker places. These are food stands with a number of tables and chairs shared by all the stands. You walk and order the food you like from the different stands and tell them where you are sitting, they will then find you and bring you the food. In Hawker places you can find anything from Chinese, to Indian, to Italian food. As far as the food goes, I am doing very well.

Please keep tuned as I will keep writing about my Penang experiences.