> ## 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.

# Confusing Number Python Solution with Tests

> Tested Python solution for LeetCode 1056 with 61 pytest cases. Generate a practice environment with lcpy.

LeetCode 1056, [Easy](/catalog/easy). Topics: [Math](/catalog/topics/math). [View on LeetCode](https://leetcode.com/problems/confusing-number/description/).

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

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

## Problem

A **confusing number** is a number that when rotated `180` degrees becomes a **different** number with each digit valid.

We can rotate digits of a number by `180` degrees to form new digits.

* When `0, 1, 6, 8,` and `9` are rotated `180` degrees, they become `0, 1, 9, 8,` and `6` respectively.
* When `2, 3, 4, 5,` and `7` are rotated `180` degrees, they become **invalid**.

Note that after rotating a number, we can ignore leading zeros.

* For example, after rotating `8000`, we have `0008` which is considered as just `8`.

Given an integer `n`, return `true` *if it is a* **confusing number**\*, or\* `false` *otherwise*.

### Examples

```
Input: n = 6
Output: true
Explanation: We get 9 after rotating 6, 9 is a valid number, and 9 != 6.
```

```
Input: n = 89
Output: true
Explanation: We get 68 after rotating 89, 68 is a valid number and 68 != 89.
```

```
Input: n = 11
Output: false
Explanation: We get 11 after rotating 11, 11 is a valid number but the value remains the same, thus 11 is not a confusing number.
```

### Constraints

* 0 \<= n \<= 10^9

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log10 n)
    # Space: O(1)
    def confusing_number(self, n: int) -> bool:
        rotated = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6]
        x, y = n, 0
        while x:
            x, digit = divmod(x, 10)
            if rotated[digit] < 0:
                return False
            y = y * 10 + rotated[digit]
        return y != n
```

## Complexity

| Time | Space |
| - | - |
| O(log10 n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.