我已经用python编写了一个简单的程序,用于检查句子是否是回文。但是我不知道为什么它不起作用。结果始终为False。有谁知道这是怎么回事?
def isPalindrome(word):
# Removes all spaces, and lowercase the word.
word = word.strip().lower()
word = word.replace(" ", "")
# If the length of the word is less than 1, means its a palindrome
if (len(word) <= 1):
return True
# Compares the first and the last character of the word.
# If it is the same, calls the function again with the same word,
# without its first and last characters. If its not the same, its
# not palindrome
else:
if word[0] == word[-1]:
isPalindrome(word[1:-1])
else:
return False
sentence = input("Enter a sentence: \n")
if (isPalindrome(sentence)):
print("The sentence %s is palindrome." % sentence)
else:
print("The sentence %s is NOT palindrome" % sentence)
您不返回函数的结果。
替换:
if word[0] == word[-1]:
isPalindrome(word[1:-1])
with
if word[0] == word[-1]:
return isPalindrome(word[1:-1])
您使这种方式变得比必须的复杂:
def palindrome(sentence):
sentence = sentence.strip().lower().replace(" ", "")
return sentence == sentence[::-1]
[sentence[::-1]
使用string slicing反转字符串中的字符。
稍微冗长的解决方案,它显示了上面return
语句的逻辑如何工作:
def palindrome(sentence):
sentence = sentence.strip().lower().replace(" ", "")
if sentence == sentence[::-1]:
return True
else:
return False
您的算法很好,唯一的问题是您没有通过递归返回真实的结果,当您递归调用它时必须返回isPalindrome结果:
else:
if word[0] == word[-1]:
return isPalindrome(word[1:-1]) #this changed
else:
return False
您需要将“ input”替换为“ raw_input”。另外,您递归地调用isPalindrome,这里也有一个错误。应该是:
if word[0] == word[-1]:
return isPalindrome(word[1:-1])
else:
return False
检查以下更正的代码:
def isPalindrome(word):
# Removes all spaces, and lowercase the word.
word = word.strip().lower()
word = word.replace(" ", "")
# If the length of the word is less than 1, means its a palindrome
if (len(word) <= 1):
return True
# Compares the first and the last character of the word.
# If it is the same, calls the function again with the same word, without its first and last characters.
# If its not the same, its not palindrome
if word[0] == word[-1]:
return isPalindrome(word[1:-1])
else:
return False
sentence = raw_input("Enter a sentence: \n")
if (isPalindrome(sentence)):
print("The sentence %s is palindrome." % sentence)
else:
print("The sentence %s is NOT palindrome" % sentence)
我假定这是一个赋值,并且递归是必要的,显然return word == word[::-1]
更简单,但实际上并不相关。您可以更简洁地编写递归函数:
def isPalindrome(word):
if not word:
return True
return word[0] == word[-1] and isPalindrome(word[1:-1])
word[0] == word[-1]
将为True
或False
,因此您将到达一个空字符串,其中not word
将为True
,因此递归结束并且该函数返回True
或word[0] == word[-1]
将为[C0 ],因此该函数将返回False
,因为将永远不会评估False
。
我也可能在函数外部进行降低:
and isPalindrome(word[1:-1])
因为已经解释了错误并且已经采用了明显的def isPalindrome(word):
if not word:
return True
return word[0] == word[-1] and isPalindrome(word[1:-1])
sentence = input("Enter a sentence: \n")
sentence = sentence.strip().lower()
sentence = sentence.replace(" ", "")
if isPalindrome(sentence):
print("The sentence %s is palindrome." % sentence)
else:
print("The sentence %s is NOT palindrome" % sentence)
,所以我将可能仅是原始版本的最小版本投入了混合:
s == s[::-1]
请注意,您不需要def isPalindrome(s):
s = s.strip().lower()
return not s or s[0] == s[-1] and isPalindrome(s[1:-1])
。现在,使用replace(" ", "")
删除外部空间,然后在strip()
中更深入地调用递归(如果我们不更早停止,因为strip()
失败,则在s[0] == s[-1]
中)删除内部空间。
在Python中检查单词的最佳方法是回文或不回文:
var[::] == var[::-1]
但是,非常重要的一点是,当您执行var[::-1]
时,Python会创建一个字符串的新副本。Python内部不知道反向操作是否会导致相同的字符串。因此,对它的编码方式是创建它的新副本。因此,当您尝试var[::1] is var[::-1]
时,您会得到FALSE
。
例如:
var = "RADAR"
var1 = var[::]
var is var1
True
var2 = var[0:6:1]
var is var2
True
var3 = var[::-1]
var is var3
False
var4 = var[-1:-6:-1]
var is var4
False
var1
'RADAR'
var2
'RADAR'
var3
'RADAR'
var4
'RADAR'
在这里您可以看到前进时,它不会创建“ RADAR”的副本,它使用相同的参考。由于PY内部了解此操作,因此将导致相同的字符串对象。但是,当您向后移动时,结果可能会有所不同。例如,如果我对“ Ethans”执行相同的操作,那么相反的操作将不相同。因此,PY不知道反转字符串的结果是什么,它会创建它的新副本。
因此,反向字符串使用'is'运算符返回假值。
这里需要注意的另一点有趣。参见以下示例:
var = "abc"
var1 = "abc"
var is var1
True
var = "Ethans Baner Pune"
var1 = "Ethans Baner Pune"
var is var1
False
我们知道字符串是不可变的,并且遵循Singleton DP,然后为什么第二种情况返回FALSE?
这是因为PY不想在速度和性能上妥协。如果您写的字符串很长并且已经存在于内存中,则PY应该引用相同的字符串。但是,发现长字符串将花费很长时间,并且性能会降低。因此,PY不用引用现有的字符串,而是创建一个新的字符串。对于整数,我们也已经理解了这一点,在整数中,它仅遵循Singleton DP方法直到限制值(256)。
让我们再看一个示例:
var = "abcdefgh"
var1 = "abcdefgh"
var is var1
True
var = "abcd efgh"
var1 = "abcd efgh"
var is var1
False