Prakhar Mohan Srivastava Prakhar Mohan Srivastava - 1 year ago 63
Python Question

Python function to check for palindrome

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):
for i in range (0, len(num)-1):
return flag

Answer Source

You should return as soon as there is a mismatch. Also, you just need to iterate till half the length:

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

BTW, you don't need that loop. You can simply do:

def palindrome(num):
    return num == num[::-1]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download