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

# Base 7 Python Solution with Tests

> Tested Python solution for LeetCode 504 with 25 pytest cases. Generate a practice environment with lcpy.

LeetCode 504, [Easy](/catalog/easy). Topics: [Math](/catalog/topics/math), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/base-7/description/).

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

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

## Problem

Given an integer `num`, return a string of its base 7 representation.

### Examples

```
Input: num = 100
Output: "202"
```

```
Input: num = -7
Output: "-10"
```

### Constraints

* -10^7 \<= num \<= 10^7

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log_7 |num|)
    # Space: O(log_7 |num|)
    def convert_to_base_7(self, num: int) -> str:
        if num == 0:
            return "0"
        negative = num < 0
        digits = ""
        value = abs(num)
        while value:
            digits = str(value % 7) + digits
            value //= 7
        return f"-{digits}" if negative else digits
```

## Complexity

| Time | Space | | | | |
| - | - | - | - | - | - |
| O(log\_7 | num | ) | O(log\_7 | num | ) |

## Tags


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