| | |
palindrome
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 148
Reputation:
Solved Threads: 32
0
#2 Nov 2nd, 2009
There is no built in function that I know of to check that.
On thing you might try doing is reversing the string and then comparing each element.
Another way to do it would be to compare the first element to the last element in your string and make sure they match the whole time. One thing that is helpful to remember when doing this is that string[-1] will give you the last element of the string, and string[-2] will give you the second to last. So if your string is racecar then inString[0] should equal inString[-1] and inString[1] should equal inString[-2] etc.
On thing you might try doing is reversing the string and then comparing each element.
python Syntax (Toggle Plain Text)
inString = 'racecar' reversestring = '' for i in range(0,len(inString)-1: reversestring.append(inString[i]
Another way to do it would be to compare the first element to the last element in your string and make sure they match the whole time. One thing that is helpful to remember when doing this is that string[-1] will give you the last element of the string, and string[-2] will give you the second to last. So if your string is racecar then inString[0] should equal inString[-1] and inString[1] should equal inString[-2] etc.
0
#3 Nov 2nd, 2009
there are a couple of posts regarding palindromes in the forum, have a look around.... but basically the algorithm is what mn_kthompson suggested
1
#6 Nov 3rd, 2009
•
•
•
•
But how could we test if the given string is pallindrome or not. I need a syntax that returns true if the string is pallindrome otherwise false.
Python Syntax (Toggle Plain Text)
>>> def is_palindrome(s): ... return s.lower() == s[::-1].lower() ... >>> is_palindrome('test') False >>> is_palindrome('racecar') True >>> is_palindrome('RACECAR') True
0
#8 Nov 3rd, 2009
or you could do it using a loop:
Python Syntax (Toggle Plain Text)
def isPalindrome( s ): s = s.lower() for i in range( len ( s ) / 2 ): if s[ i ] != s[ -1 -i ]: return False return True >>> isPalindrome( "RACECAr" ) True >>> isPalindrome( "test" ) False >>> isPalindrome( "civiC" ) True >>> isPalindrome( "ciniC" ) True >>> isPalindrome( "cinac" ) False >>>
Last edited by masterofpuppets; Nov 3rd, 2009 at 8:49 am.
![]() |
Similar Threads
- To form a palindrome of a given string (C)
- Help on stack , queue, palindrome program... (C++)
- palindrome program (Legacy and Other Languages)
- stack palindrome problem? (C++)
Other Threads in the Python Forum
- Previous Thread: Palindrome Checking (Python)
- Next Thread: Please help me get past this problem
Views: 482 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for Python
alarm aliased application beginner calculator casino character code command cursor cx-freeze definedlines development dictionary dynamic error event examples excel exe file filename float format ftp function google graphics gui homework ideas import input java launcher line linux list lists logging loop matching microphone mouse movingimageswithpygame newb number numbers obexftp output parsing path permissions phonebook port prime program programming projects py2exe pygame pygtk pyqt pysimplewizard python random recursion recursive refresh return reverse scrolledtext shebang simple skinning sprite ssh statistics string strings table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip windows wxpython






