Skip to content

Commit 9643988

Browse files
r0sa2sedatguzelsemme
authored andcommitted
Added is_palindrome.py (TheAlgorithms#8748)
1 parent c4fdf34 commit 9643988

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

maths/is_palindrome.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def is_palindrome(num: int) -> bool:
2+
"""
3+
Returns whether `num` is a palindrome or not
4+
(see for reference https://en.wikipedia.org/wiki/Palindromic_number).
5+
6+
>>> is_palindrome(-121)
7+
False
8+
>>> is_palindrome(0)
9+
True
10+
>>> is_palindrome(10)
11+
False
12+
>>> is_palindrome(11)
13+
True
14+
>>> is_palindrome(101)
15+
True
16+
>>> is_palindrome(120)
17+
False
18+
"""
19+
if num < 0:
20+
return False
21+
22+
num_copy: int = num
23+
rev_num: int = 0
24+
while num > 0:
25+
rev_num = rev_num * 10 + (num % 10)
26+
num //= 10
27+
28+
return num_copy == rev_num
29+
30+
31+
if __name__ == "__main__":
32+
import doctest
33+
34+
doctest.testmod()

0 commit comments

Comments
 (0)