Skip to content

Commit 12bcdb8

Browse files
authored
Refactor palindrome check to O(1) space mathematical approach
1 parent b1779c5 commit 12bcdb8

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

maths/palindrome_number.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
def is_palindrome(number: int) -> bool:
22
"""
3-
Determines if an integer is a palindrome without string conversion.
3+
Determines if an integer is a palindrome without using string conversion.
44
55
Logic:
6-
1. Filter out negative numbers and multiples of 10.
7-
2. Reverse the second half of the number.
8-
3. Compare the two halves.
6+
1. Negative numbers are not palindromes.
7+
2. Numbers ending in 0 (except 0 itself) are not palindromes.
8+
3. Reverse half of the number and compare.
9+
10+
Examples:
11+
>>> is_palindrome(121)
12+
True
13+
>>> is_palindrome(12321)
14+
True
15+
>>> is_palindrome(10)
16+
False
17+
>>> is_palindrome(-121)
18+
False
19+
>>> is_palindrome(0)
20+
True
921
"""
1022
if number < 0 or (number % 10 == 0 and number != 0):
1123
return False

0 commit comments

Comments
 (0)