> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Palindrome Number Python Solution with Tests

> Tested Python solution for LeetCode 9 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 9, Easy. Topics: Math. [View on LeetCode](https://leetcode.com/problems/palindrome-number/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 9   # by problem number
lcpy gen -s palindrome_number   # by problem name
```

## Problem

Given an integer `x`, return `true` if `x` is a **palindrome**, and `false` otherwise.

### Examples

```
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
```

```
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
```

```
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
```

### Constraints

* -2^31 \<= x \<= 2^31 - 1

**Follow up:** Could you solve it without converting the integer to a string?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/palindrome_number/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/palindrome_number/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log10(n)) - process half the digits
    # Space: O(1)
    def is_palindrome(self, x: int) -> bool:
        # 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

        # Even length: x == reversed_half
        # Odd length: x == reversed_half // 10 (drop middle digit)
        return x == reversed_half or x == reversed_half // 10
```

## Complexity

| Time                                  | Space |
| ------------------------------------- | ----- |
| O(log10(n)) - process half the digits | O(1)  |

## Tags

[Grind](/catalog/grind), [NeetCode All](/catalog/neetcode).
