| | |
palindrome
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 141
Reputation:
Solved Threads: 32
0
#2 24 Days Ago
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 24 Days Ago
there are a couple of posts regarding palindromes in the forum, have a look around.... but basically the algorithm is what mn_kthompson suggested
•
•
Join Date: Aug 2008
Posts: 148
Reputation:
Solved Threads: 46
1
#6 24 Days Ago
•
•
•
•
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 24 Days Ago
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; 24 Days Ago 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
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






