| | |
Odd- Length Palindrome
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
The following code should return the longest odd-length palindrome in a string centered at the index. So in a string 'asradarer' it should return 'radar'. Can you explain to me what the problem here is?
Python Syntax (Toggle Plain Text)
def get_odd_palindrome_at(word, index): num = 1 new_word = "" while index < len(word): if word[index - num] == word[index + num]: print new_word + word[index - num:index + (num + 1)] num += 1 else: print ""
•
•
Join Date: Jun 2008
Posts: 128
Reputation:
Solved Threads: 31
0
#3 Oct 25th, 2009
Your description is incorrect. This function will never return 'radar' on any string input, because it needs another parameter.
Your loop does not exit. You increment num, but the loop's exit expression does not depend on it.
It possibly exits on index error if num becomes too large. At first sight.
The code has indent error. The else row is loose.
The else clause is redundant without a break statement in the loop. Unless you use exceptions for something weird.
I suggest you read the problem you are solving once more. And do not forget to read your own posts before hitting the submit button.
Your loop does not exit. You increment num, but the loop's exit expression does not depend on it.
It possibly exits on index error if num becomes too large. At first sight.
The code has indent error. The else row is loose.
The else clause is redundant without a break statement in the loop. Unless you use exceptions for something weird.
I suggest you read the problem you are solving once more. And do not forget to read your own posts before hitting the submit button.
0
#4 Oct 25th, 2009
Your problem stems from the fact that you never give index an updated value in the while loop. I would use the while loop this way ...
python Syntax (Toggle Plain Text)
def get_odd_palindrome_at(word, index): num = 1 new_word = "" while True: if index + num > len(word) - 1: break if word[index - num] == word[index + num]: print new_word + word[index - num:index + (num + 1)] num += 1 else: print "" word = 'asradarer' print len(word) # test index = len(word)//2 get_odd_palindrome_at(word, index)
May 'the Google' be with you!
![]() |
Similar Threads
- Starting Python (Python)
- Diamond??? (C++)
- LC-3 Palindrome Help (Computer Science)
- Argorithm: palindromes, write recursive function. (Computer Science)
- not understanding the practice problems (C++)
- Palindrome help (Java)
- Recursive Palindrome (Java)
- Recursive function - checking for palindromes (C)
- my first recursion program... :/ (C)
Other Threads in the Python Forum
- Previous Thread: File handling - reading filenames
- Next Thread: Help with two questions
| Thread Tools | Search this Thread |
Tag cloud for Python
ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog data decimals dictionary drive dynamic error examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse newb number numbers output parsing path pointer port prime program programming progressbar projects push py2exe pygame pyqt python random recursion recursive refresh schedule screensaverloopinactive script scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






