这是获取回文的正确方法吗?我得到了结果,但不确定
我试图剥离“永不奇怪甚至”以删除空格,但它没有起作用,或者我没有正确地进行操作。
因此,我试图将字符串分为列表,然后加入它们,以使所有空格都将被删除。
这是代码:
def is_palindrome(input_string):
new_string = ""
reverse_string = ""
lowerinput = input_string.lower()
splitinginput = lowerinput.split()
for new_string in lowerinput:
if lowerinput[0] == lowerinput[-1]:
new = new_string.join(splitinginput)
reverse_string = new_string[::-1]
if new_string == reverse_string:
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
对不起,我是新来的,这个问题可能很幼稚,但我很想知道您的反馈。
I tried to strip the "Never Odd or Even" to remove the whitespaces but it didn't worked or i was not doing it correctly.
So I tried to split the string into list and then join them such that all the whitespace would be removed.
here is the code :
def is_palindrome(input_string):
new_string = ""
reverse_string = ""
lowerinput = input_string.lower()
splitinginput = lowerinput.split()
for new_string in lowerinput:
if lowerinput[0] == lowerinput[-1]:
new = new_string.join(splitinginput)
reverse_string = new_string[::-1]
if new_string == reverse_string:
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
Sorry i am new and this question could be little naive but i would love to know your feedback..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的解决方案
this is my solution
您可以在不使用循环的情况下检查palindrome,
输出:
You can check for palindrome without using loops
output: