MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / is_int_palindrome

Function is_int_palindrome

maths/is_int_palindrome.py:1–28  ·  view source on GitHub ↗

Returns whether `num` is a palindrome or not (see for reference https://en.wikipedia.org/wiki/Palindromic_number). >>> is_int_palindrome(-121) False >>> is_int_palindrome(0) True >>> is_int_palindrome(10) False >>> is_int_palindrome(11) True >>> is_int_p

(num: int)

Source from the content-addressed store, hash-verified

1def is_int_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_int_palindrome(-121)
7 False
8 >>> is_int_palindrome(0)
9 True
10 >>> is_int_palindrome(10)
11 False
12 >>> is_int_palindrome(11)
13 True
14 >>> is_int_palindrome(101)
15 True
16 >>> is_int_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
31if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected