mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-02 17:31:08 +00:00
Added is_palindrome.py (#8748)
This commit is contained in:
parent
9b3e4028c6
commit
cf5e34d479
34
maths/is_palindrome.py
Normal file
34
maths/is_palindrome.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
def is_palindrome(num: int) -> bool:
|
||||||
|
"""
|
||||||
|
Returns whether `num` is a palindrome or not
|
||||||
|
(see for reference https://en.wikipedia.org/wiki/Palindromic_number).
|
||||||
|
|
||||||
|
>>> is_palindrome(-121)
|
||||||
|
False
|
||||||
|
>>> is_palindrome(0)
|
||||||
|
True
|
||||||
|
>>> is_palindrome(10)
|
||||||
|
False
|
||||||
|
>>> is_palindrome(11)
|
||||||
|
True
|
||||||
|
>>> is_palindrome(101)
|
||||||
|
True
|
||||||
|
>>> is_palindrome(120)
|
||||||
|
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()
|
Loading…
Reference in New Issue
Block a user