| | |
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
Views: 662 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Python
address anydbm app bash beginner changecolor cipher class clear client code conversion coordinates corners curves definedlines development dictionary dynamic events examples excel feet file float format ftp function gui handling homework images import input java keycontrol line linux list lists loan loop matching microcontroller mouse newb number numbers output parsing path permissions port prime program programming projects py2exe pygame pymailer pyqt python random rational raw_input recursion recursive scrolledtext searchingfile shebang singleton split ssh string strings table tails terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable windows word wx.wizard wxpython xlwt






