File: thousand-separator/solution.py

Date: 2026-06-06

Time: 19:28

thousand-separator/solution.py

Purpose

This file implements the solution for LeetCode 1556 — Thousand Separator. It formats a non-negative integer by inserting dots (.) every three digits from the right, mimicking locale-specific thousand separators (e.g., 1234567"1.234.567").

Key Components

Solution.thousand_separator(self, n: int) -> str — the sole method. Takes a non-negative integer and returns its dot-separated string representation.

Flow

1. Convert n to its string form s.

2. Repeatedly chop 3-character chunks off the right end of s, appending each chunk to chunks.

3. Whatever remains (1–3 characters) becomes the final chunk — this is the leftmost group.

4. Reverse chunks (they were collected right-to-left) and join with ".".

For n = 1234567:

Patterns

Right-to-left chunking via slicing — instead of using modular arithmetic or Python's format specifiers (f"{n:,}".replace(",", ".")), the solution manually partitions the string. This is a common pattern in LeetCode solutions that avoids reliance on locale or format-spec behavior to demonstrate the algorithm explicitly.

Dependencies

Imports: None — pure stdlib, no external dependencies.

Imported by: thousand-separator/test_solution.py (directly). The massive "Imported By" list in the prompt is likely an artifact of how the test harness resolves imports across the repo — those other test files don't actually import this solution; they share a common test runner infrastructure.

Invariants

Error Handling

None. The method trusts its caller to provide a valid non-negative integer. No type checking, no bounds validation — appropriate for a LeetCode submission where the problem constraints guarantee valid input.

Topics to Explore

Beliefs