""" 0009.1 - Palindrome Number - Solution 1 - String Conversion """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isPalindrome(self, x: int) -> bool:
"""Palindrome Number Function"""
return str(x) == str(x)[::-1]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isPalindrome(121)) # True
print(Solution().isPalindrome(-121)) # False
print(Solution().isPalindrome(10)) # False
print(Solution().isPalindrome(0)) # True
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0009.2 - Palindrome Number - Solution 2 - Reverse Half """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isPalindrome(self, x: int) -> bool:
"""Palindrome Number Function"""
# Negative numbers and numbers ending in 0 (except 0 itself) are not palindromes
if x < 0 or (x % 10 == 0 and x != 0):
return False
reversed_half = 0
while x > reversed_half:
reversed_half = reversed_half * 10 + x % 10
x //= 10
# For even-length numbers: x == reversed_half
# For odd-length numbers: x == reversed_half // 10 (drop the middle digit)
return x == reversed_half or x == reversed_half // 10
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isPalindrome(121)) # True
print(Solution().isPalindrome(-121)) # False
print(Solution().isPalindrome(10)) # False
print(Solution().isPalindrome(0)) # True
print(Solution().isPalindrome(12321)) # True
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()