2023-05-26 06:50:33 +00:00
|
|
|
def is_int_palindrome(num: int) -> bool:
|
2023-05-18 23:48:22 +00:00
|
|
|
"""
|
|
|
|
Returns whether `num` is a palindrome or not
|
|
|
|
(see for reference https://en.wikipedia.org/wiki/Palindromic_number).
|
|
|
|
|
2023-05-26 06:50:33 +00:00
|
|
|
>>> is_int_palindrome(-121)
|
2023-05-18 23:48:22 +00:00
|
|
|
False
|
2023-05-26 06:50:33 +00:00
|
|
|
>>> is_int_palindrome(0)
|
2023-05-18 23:48:22 +00:00
|
|
|
True
|
2023-05-26 06:50:33 +00:00
|
|
|
>>> is_int_palindrome(10)
|
2023-05-18 23:48:22 +00:00
|
|
|
False
|
2023-05-26 06:50:33 +00:00
|
|
|
>>> is_int_palindrome(11)
|
2023-05-18 23:48:22 +00:00
|
|
|
True
|
2023-05-26 06:50:33 +00:00
|
|
|
>>> is_int_palindrome(101)
|
2023-05-18 23:48:22 +00:00
|
|
|
True
|
2023-05-26 06:50:33 +00:00
|
|
|
>>> is_int_palindrome(120)
|
2023-05-18 23:48:22 +00:00
|
|
|
False
|
|
|
|
"""
|
|
|
|
if num < 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
num_copy: int = num
|
|
|
|
rev_num: int = 0
|
|
|
|
while num > 0:
|
|
|
|
rev_num = rev_num * 10 + (num % 10)
|
|
|
|
num //= 10
|
|
|
|
|
|
|
|
return num_copy == rev_num
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|