[英]Recursive Program: What am I doing wrong?
我正在尝试编写代码来分析单词是否是回文。 顺便说一句,回文是向后和向前阅读相同的单词。 例如“女士”或“中午”
尝试一下:
x = raw_input("please enter a word:\n")
L = len(x)
# this part returns the first letter of the word
def first(word):
return word[0]
# this part returns the last letter of the word
def last(word):
return word[-1]
def middle(word):
return word[1:-1]
def is_palindrome(word):
if L <= 2:
print 'enter a word with at least three letters'
elif first(word) != last(word):
print 'This word is not a palindrome'
else:
word = middle(word)
is_palindrome(word)
is_palindrome(x)
但是当执行时,我得到
IndexError: string index out of range
...line 7, in first return word[0]
“ is_palindrome”的第一个分支工作正常。 即,当单词不是回文时,我不会出错。 像“ noopn”一样执行没有错误,但是错误在第二个分支中
我已经玩了很多次此代码,但无法弄清楚“迭代部分”,我已经找到了答案,但是我不想看它。 我需要弄清楚两件事:1.一种使is_palindrome函数中的迭代正确运行的方法吗? 2.最终退出程序的方法。
你们能否在不提供解决方案的情况下指导我如何回答这些问题?
最后我应该在哪里写上打印说明:print'这个词是回文'
谢谢
为了实现您的目标,为什么不仅仅使用:
string[::-1] == string
您回答的原因是只有1个字母时, middle
将返回一个空字符串,然后''[0]
将导致错误。
您需要一个基本案例进行递归。 一个字母的单词是回文,而空字符串也是回文。
def is_palindrome(word):
# handle the base case
if len(word) <= 1:
print 'This word is a palindrome'
elif first(word) != last(word):
print 'This word is not a palindrome'
else:
word = middle(word)
is_palindrome(word)
如果要拒绝少于三个字母的单词,则可以使用调用递归函数的帮助器函数:
def is_palindromeHelper(word):
if len(word) <= 2:
print 'enter a word with at least three letters'
else:
is_palindrome(word)
就个人而言,我更喜欢将支票和输出分开。 因此is_palindrome()
应该只返回答案,而不负责告诉用户。 这使其更具可重用性。
def is_palindrome(word):
# handle the base case
if len(word) <= 1:
return True
elif first(word) != last(word):
return False
else:
word = middle(word)
return is_palindrome(word)
这使您能够
x = raw_input("please enter a word:\n")
L = len(x)
if L <= 2:
print 'enter a word with at least three letters'
elif is_plaindrome(word):
print 'This word is a palindrome'
else:
print 'This word is not a palindrome'
这将有效性检查放在执行的最前面,而在递归中,只有在整个递归中有效的检查。
(我怀疑,如果你的检查是必要的话-是y
并oo
?没有回文我们可以争论空字符串,但是......)
接下来的改进步骤可能是忽略函数first()
, last()
和middle()
-它们是微不足道的,并且仅使用一次,因此您可以在其中放置代码。
在您的代码中添加了一个额外的条件,它将解决您的问题。 如果只剩下一个字符,则无需调用is_palindrome()。
x = raw_input("please enter a word:\n")
L = len(x)
# this part returns the first letter of the word
def first(word):
return word[0]
# this part returns the last letter of the word
def last(word):
return word[-1]
def middle(word):
return word[1:-1]
def is_palindrome(word):
if L <= 2:
print 'enter a word with at least three letters'
elif first(word) != last(word):
print 'This word is not a palindrome'
else:
word = middle(word)
if len(word) > 1:
is_palindrome(word)
else:
print 'This word is a palindrome'
is_palindrome(x)
不建议使用大写和空白的基本版本,我建议:
def is_palindrome(word):
if len(word) < 3:
print 'Enter a word with at least three letters'
else:
for letter in range(len(word)/2):
if word[letter] != word[-letter - 1]:
print "This word is not a palindrome"
return
print "This word is a palindrome"
虽然我认为它可能会亲自删除空白并使用.lower()进行比较。 然后它将是不区分大小写的,并且还可以测试短语或句子。
好方向的家伙。 所有人都投票赞成。
这些指示使我可以编写以下简明代码
代码1
x = raw_input("enter a word to check if it is a palindrome:\n")
if x[::-1] == x:
print 'yes this one is a palindrome'
else:
print 'sorry try again by re-running the program'
代码2
x = raw_input("enter a word to check if it is a palindrome:\n")
if len(x) <= 1:
print 'Of course ', x, ' is a palindrome'
def is_palindrome(x):
if len(x) >= 2:
if x[0]!=x[-1]:
print 'This is not a palindrome'
else:
x = x[1:-1]
return is_palindrome(x)
print 'This is FINALLY a real palindrome'
is_palindrome(x)
我想我可以将is_palindrome函数作为条件语句len(x)<= 1的第二个分支包括在内,但是我更喜欢这种方式,因为代码与该函数有关
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.