Palindrome Number

Key Idea

Solution

class Solution:
    def isPalindrome(self, x: int) -> bool:
        # Inputs: int
        # Outputs: bool
        # Description: if x is palindrome (read same forward/backward), true, if not false

        # Example 
        # Input: 121
        # Output: true
        
        # Example
        # Input: -121
        # Output: False

        # Example 
        # Input: 10
        # Output: false

        # Negative
        if x < 0:
            return False
        
        # Initalize
        str_x = str(x)
        final_index = len(str_x)

        # Iterate
        for index,digit in enumerate(str_x[:round(final_index/2)]):
            if digit != str_x[final_index -1]:
                return False
            # Move final index forward
            final_index -= 1
        return True

Complexity