Odd- Length Palindrome

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 18
Reputation: pyprog is an unknown quantity at this point 
Solved Threads: 0
pyprog pyprog is offline Offline
Newbie Poster

Odd- Length Palindrome

 
0
  #1
Oct 24th, 2009
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?
  1. def get_odd_palindrome_at(word, index):
  2. num = 1
  3. new_word = ""
  4. while index < len(word):
  5. if word[index - num] == word[index + num]:
  6. print new_word + word[index - num:index + (num + 1)]
  7. num += 1
  8. else:
  9. print ""
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #2
Oct 25th, 2009
Well, what's happening, what's going wrong with the program? Could you post the error message.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 128
Reputation: slate is an unknown quantity at this point 
Solved Threads: 31
slate slate is offline Offline
Junior Poster
 
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
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 ...
  1. def get_odd_palindrome_at(word, index):
  2. num = 1
  3. new_word = ""
  4. while True:
  5. if index + num > len(word) - 1:
  6. break
  7. if word[index - num] == word[index + num]:
  8. print new_word + word[index - num:index + (num + 1)]
  9. num += 1
  10. else:
  11. print ""
  12.  
  13. word = 'asradarer'
  14. print len(word) # test
  15. index = len(word)//2
  16. get_odd_palindrome_at(word, index)
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC