Skip to content

Instantly share code, notes, and snippets.

@mtovmassian
Created February 14, 2021 22:49
Show Gist options
  • Save mtovmassian/db2020d0c608515e25616f0eda979f25 to your computer and use it in GitHub Desktop.
Save mtovmassian/db2020d0c608515e25616f0eda979f25 to your computer and use it in GitHub Desktop.
Mathematically check if a number is a palindrome
def extract_digits_left(num):
if num < 10:
return [num]
else:
return [*extract_digits_left(num // 10), num % 10]
def extract_digits_right(num):
if num < 10:
return [num]
else:
return [num % 10, *extract_digits_right(num // 10)]
def is_palindrome(num):
return extract_digits_left(num) == extract_digits_right(num)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment