File: distribute-candies-to-people/solution.py

Date: 2026-06-06

Time: 16:24

Purpose

This file implements the solution to LeetCode 1103 — Distribute Candies to People. It owns exactly one responsibility: given a total candy count and a number of people, distribute candies in rounds of increasing amounts, wrapping around the row until all candies are exhausted.

Key Components

distributecandies(candies: int, numpeople: int) -> list[int]

The sole function. Contract:

Patterns

Simulation pattern — rather than deriving a closed-form formula (which is possible via quadratic math), this solves the problem by directly simulating the distribution process. The give counter tracks how many candies the current turn *wants* to give (1, 2, 3, ...), and (give - 1) % num_people maps that 1-based turn number to a 0-based person index, achieving the circular wrap-around.

The min(give, candies) idiom handles the partial-last-give edge case in one expression — no separate "last round" branch needed.

Dependencies

Imports: None. Pure stdlib Python, no external dependencies.

Imported by: The distribute-candies-to-people/test_solution.py file imports this function. The massive "Imported By" list in the prompt is an artifact of the repo's test infrastructure — those other test files don't actually import *this* function; they follow the same import pattern for their own solutions.

Flow

1. Initialize result as a zero-filled list of length num_people.

2. Start give = 1 (first person gets 1 candy).

3. Loop while candies > 0:

4. Return the accumulated result.

Invariants

Error Handling

None. The function trusts its caller to provide valid inputs (non-negative candies, positive numpeople). Passing numpeople = 0 would cause a ZeroDivisionError from the modulo. Passing negative candies returns an all-zeros list (the while loop never enters).

Topics to Explore

Beliefs