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

# Integer to English Words Python Solution

> Tested Python solution for LeetCode 273 with 32 pytest cases. Generate a practice environment with lcpy.

LeetCode 273, [Hard](/catalog/hard). Topics: [Math](/catalog/topics/math), [String](/catalog/topics/string), [Recursion](/catalog/topics/recursion). [View on LeetCode](https://leetcode.com/problems/integer-to-english-words/description/).

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

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

## Problem

Convert a non-negative integer `num` to its English words representation.

### Examples

```
Input: num = 123
Output: 'One Hundred Twenty Three'
```

```
Input: num = 12345
Output: 'Twelve Thousand Three Hundred Forty Five'
```

```
Input: num = 1234567
Output: 'One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven'
```

### Constraints

* 0 \<= num \<= 2^31 - 1

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(1)  # num < 2^31 bounds the word count
    # Space: O(1)
    BELOW_20: tuple[str, ...] = (
        "",
        "One",
        "Two",
        "Three",
        "Four",
        "Five",
        "Six",
        "Seven",
        "Eight",
        "Nine",
        "Ten",
        "Eleven",
        "Twelve",
        "Thirteen",
        "Fourteen",
        "Fifteen",
        "Sixteen",
        "Seventeen",
        "Eighteen",
        "Nineteen",
    )
    TENS: tuple[str, ...] = (
        "",
        "",
        "Twenty",
        "Thirty",
        "Forty",
        "Fifty",
        "Sixty",
        "Seventy",
        "Eighty",
        "Ninety",
    )
    THOUSANDS: tuple[str, ...] = ("", "Thousand", "Million", "Billion")

    def _three_digit(self, num: int) -> list[str]:
        """Convert 0 <= num < 1000 to words, e.g. 123 -> ['One', 'Hundred', ...]."""
        if num == 0:
            return []
        if num < 20:
            return [self.BELOW_20[num]]
        if num < 100:
            words = [self.TENS[num // 10]]
            if num % 10:
                words.append(self.BELOW_20[num % 10])
            return words
        words = [self.BELOW_20[num // 100], "Hundred"]
        words.extend(self._three_digit(num % 100))
        return words

    def number_to_words(self, num: int) -> str:
        if num == 0:
            return "Zero"
        words: list[str] = []
        for unit in range(3, -1, -1):
            chunk = num // 1000**unit
            if chunk:
                words.extend(self._three_digit(chunk))
                if unit:
                    words.append(self.THOUSANDS[unit])
                num %= 1000**unit
        return " ".join(words)
```

## Complexity

| Time | Space |
| - | - |
| O(1)  # num \< 2^31 bounds the word count | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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