4

Given below is the code to check if a list is a palindrome or not. It is giving correct output for 983. Where am I going wrong?

def palindrome(num):
    flag=0
    r=num[::-1]
    for i in range (0, len(num)-1):
        if(r[i]==num[i]):
            flag=1
        else:
            flag=0
    return flag
4

10 回答 10

23

一旦出现不匹配,您应该立即返回。此外,您只需要迭代到一半的长度:

def function(...):
    ...
    for i in range (0, (len(num) + 1) / 2):
        if r[i] != num[i]:
            return False
    return True

顺便说一句,你不需要那个循环。你可以简单地做:

def palindrome(num):
    return num == num[::-1]
于 2013-10-14T16:43:53.690 回答
4

这会更容易:

def palindrome(num):
    if num[::-1] == num:
       return True
    else:
       return False
于 2013-10-14T16:44:46.833 回答
1

您的for循环检查所有字符对,无论是否发现不匹配。因此,在字符串 '38113' 的情况下,它将返回,True因为在检查 '38113' 中最后一个数字的相等性及其反转版本 '31183' (两者都等于 3,而字符串不是 ' t 回文)。因此,您需要在发现不匹配后 立即返回;如果您检查了所有字符但没有找到 - 然后 return ,如下所示: flagTrue
FalseTrue

def palindrome(num):
    r = num[::-1]
    for i in range (0, len(num)-1):
        if(r[i] != num[i]):
            return False
    return True  

此外,正如有人指出的那样,使用 python 的切片会更好——查看文档

于 2013-10-14T17:00:40.943 回答
0

只是为了记录,对于那些寻找一种更算法的方法来验证给定字符串是否是回文的人,有两种实现相同的方法(使用whilefor循环):

def is_palindrome(word):

    letters = list(word)    
    is_palindrome = True
    i = 0

    while len(letters) > 0 and is_palindrome:       
        if letters[0] != letters[-1]:
            is_palindrome = False
        else:
            letters.pop(0)
            if len(letters) > 0:
                letters.pop(-1)

    return is_palindrome

还有....第二个:

def is_palindrome(word):

    letters = list(word)
    is_palindrome = True

    for letter in letters:
        if letter == letters[-1]:
            letters.pop(-1)
        else:
            is_palindrome = False
            break

    return is_palindrome
于 2016-06-13T05:27:03.947 回答
0
str1=str(input('enter string:'))
save=str1
revstr=str1[::-1]
if save==revstr:
     print("string is pailandrom")
else:
     print("not pailadrom")
于 2016-08-08T11:29:05.313 回答
0

这会容易得多:

def palindrome(num):
    a=num[::-1]
    if num==a:
        print (num,"is palindrome")
    else:
        print (num,"is not palindrome")

x=input("Enter to check palindrome:")
palindrome(x)
于 2017-06-25T12:12:07.427 回答
0
# We are taking input from the user.
# Then in the function we are reversing the input i.e a using 
# slice     [::-1] and 
# storing in b
# It is palindrome if both a and b are same.

a = raw_input("Enter to check palindrome:") 
def palin():
    #Extended Slices to reverse order.
    b = a[::-1]
    if a == b:
        print "%s is palindrome" %a
    else:
        print "%s is not palindrome" %a
palin()
于 2017-02-06T11:49:39.087 回答
0

我认为这是最优雅的:

def is_palindrome(s):
    if s != '':
        if s[0] != s[-1]:
            return False
        return is_palindrome(s[1:-1])
    return True

它也是 is_palindrome() 函数中的相同代码:

pip install is-palindrome


>>> from is_palindrome import is_palindrome
>>> x = "sitonapanotis"
>>> y = is_palindrome(x)
>>> y
True

安装与导入时请注意连字符与下划线

于 2019-03-05T04:35:10.200 回答
-1
a="mom"
b='mom'[::-1]  # reverse the string
if a==b:  # if original string equals to reversed
    print ("palindrome ")
else:
    print ("not a palindrome ")
于 2017-07-12T12:06:11.923 回答
-2
def palindrome(a):
     a=raw_input('Enter :')
     b=a[::-1]
     return a==b
于 2014-07-04T11:25:23.960 回答