0

Please tell me what is wrong with my code. The function returned False for the palindrome(11) and True for some cases.

Below is the code :

def palindrome(x):
    if(x < 0 or (x%10 == 0 and x != 0)):
        return False

    revertedNumber = 0
    while(x > revertedNumber):
        revertedNumber = revertedNumber * 10 + x%10
        x = x/10

    return(x == revertedNumber or x == revertedNumber/10)

Above code returned false for the input 11

Victor M Perez
  • 2,185
  • 3
  • 19
  • 22
Siddy Mta
  • 43
  • 5
  • 1
    Python 3 two kinds of division: division resulting in floats `/` and division resulting in ints `//`. I suspect you were looking for the latter – Patrick Haugh Jan 13 '18 at 15:29
  • 2
    On Python 2, `palindrome(11)` returns `True`, but it returns `False` on Python 3. You should be using `//` floor division, then it will work correctly on both versions. – PM 2Ring Jan 13 '18 at 15:30
  • Just tested according to the comments above. See this link with the results https://repl.it/@vperezher/TriangularWornUpupa – Victor M Perez Jan 13 '18 at 15:41
  • Thanks a lot. I forgot python uses two types of division. – Siddy Mta Jan 13 '18 at 15:43
  • FWIW, we've been able to invoke Python 3 division behaviour via a `__future__` import since Python 2.2, so there's no good excuse for using `/` when you need integer division. – PM 2Ring Jan 13 '18 at 15:47
  • Also see https://stackoverflow.com/questions/2958684/python-division and the various linked questions there, as well as the questions linked in the duplicate target. – PM 2Ring Jan 13 '18 at 15:54

0 Answers0