Skip to content
Open
Changes from 4 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
18 changes: 18 additions & 0 deletions maths/palindrome_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 string conversion.

Logic:
1. Filter out negative numbers and multiples of 10.
2. Reverse the second half of the number.
3. Compare the two halves.
"""
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