discussion

BartC bc at freeuk.com
Fri Oct 1 09:46:39 EDT 2010


"Geo_subodh" <gcontsubodh at gmail.com> wrote in message 
news:31a08825-bb72-4e9f-8710-a39fe2bc9216 at u31g2000pru.googlegroups.com...
> please send me the simple python code that uses input number greater
> than3 digits(>3 digits) and  checks whether the number is palindrome
> or not.

The following works without using strings (although leading zeros are 
ignored, so that 01210 returns False):

def fpalindrome(n):
        if n<0: return False
        if n<10: return True

        digits=1
        a=n
        while a>=10:
                digits=digits+1
                a=a//10

        lastdigit=n%10
        firstdigit=n//(10**(digits-1))

        if firstdigit!=lastdigit: return False
        if digits==2: return True

        middledigits=n//10-firstdigit*(10**(digits-2))

        return fpalindrome(middledigits)

print fpalindrome(12345678987654321)

-- 
Bartc 




More information about the Python-list mailing list