Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions maths/palindrome_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def is_palindrome(number: int) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file maths/palindrome_number.py, please provide doctest for the function is_palindrome

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added required doctests for is_palindrome as requested. Please review.

"""
Determines if an integer is a palindrome without using string conversion.

Logic:
1. Negative numbers are not palindromes.
2. Numbers ending in 0 (except 0 itself) are not palindromes.
3. Reverse half of the number and compare.

Examples:
>>> is_palindrome(121)
True
>>> is_palindrome(12321)
True
>>> is_palindrome(10)
False
>>> is_palindrome(-121)
False
>>> is_palindrome(0)
True
"""
if number < 0 or (number % 10 == 0 and number != 0):
return False

reversed_half = 0
while number > reversed_half:
reversed_half = (reversed_half * 10) + (number % 10)
number //= 10

return number == reversed_half or number == reversed_half // 10