Created
February 14, 2021 22:49
-
-
Save mtovmassian/db2020d0c608515e25616f0eda979f25 to your computer and use it in GitHub Desktop.
Mathematically check if a number is a palindrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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