We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c4fdf34 commit 9643988Copy full SHA for 9643988
maths/is_palindrome.py
@@ -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
12
+ >>> is_palindrome(11)
13
14
+ >>> is_palindrome(101)
15
16
+ >>> is_palindrome(120)
17
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