8 Nov 2020

4.9 Check decimal integer is a palindrome

Problem: Write a program that takes an integer and determines if that integer’s representation as decimal string is a palindrome(negative integer isn’t palindrome.).

def is_palindrome(i_num):
  if i_num < 0:
    return False
  most_left = 1
  while most_left*10 < i_num:
    most_left *= 10
  while most_left > 1:
    if i_num // most_left != i_num % 10:
      return False
    i_num //= 10
    i_num %= most_left
    most_left = most_left//100
  return True

Tags: