View Single Post
Old 12-23-2010, 06:18 AM   #4
Tonsko
Junior Member

Activity Longevity
0/20 11/20
Today Posts
0/11 ssssssss8
Location: UK
Default

Quote:
Originally Posted by dmmik View Post
Here is a whiteboard C coding question I was asked at an actual game company interview. Complete this function:

Code:
bool IsPalendrome(char* str)
{

}
Apologies for thread resurrection.

I saw this last night while browsing, and it's a nice little problem, if not especially taxing. As a novice, I originally thought it would be best to break it down into an array and do something complicated such as reversing the order of the array and then comparing them. But there is no need - after further reading, having seen the string tools available. Once I was thinking along those lines, it took about 20 mins.

So here's a solution (in python rather than C) - anyone got any optimisation tips?

Code:
#!/usr/bin/python

def isPalindrome(string):
    length = len(string)
    #print length
    
    i=0
    j=length
    
    while j > 0:
    	if string[i] == string[j-1]:
    		#print string[i],string[j-1]
    		i=i+1
    		j=j-1
    	else:
		print 'Not a palindrome'
		break
    	
    	if j == 0:
    		print 'Word is a palindrome'
    	else:
    		pass
    		
input = raw_input()
isPalindrome(input)
The mild advantage of this is that it's not typed, so works equally well with palindromic numbers. For the c problem, I guess you'd have to put in some type checking to sanitise the input before doing anything.

Last edited by Tonsko : 12-23-2010 at 06:24 AM.
Tonsko is offline