TheAlgorithms/Python · error · ValueError

All days elements should be less than 366

Error message

All days elements should be less than 366

What it means

Raised by mincost_tickets() when any travel day is >= 366. The DP function dynamic_programming(index) terminates at index > 365, so the model only covers a single non-leap year (days 1..365). Day 366 or beyond would fall outside the memoized recursion and is rejected.

Source

Thrown at dynamic_programming/minimum_tickets_cost.py:105

     ...
    ValueError: The parameter costs should be a list of three integers
    """

    # Validation
    if not isinstance(days, list) or not all(isinstance(day, int) for day in days):
        raise ValueError("The parameter days should be a list of integers")

    if len(costs) != 3 or not all(isinstance(cost, int) for cost in costs):
        raise ValueError("The parameter costs should be a list of three integers")

    if len(days) == 0:
        return 0

    if min(days) <= 0:
        raise ValueError("All days elements should be greater than 0")

    if max(days) >= 366:
        raise ValueError("All days elements should be less than 366")

    days_set = set(days)

    @functools.cache
    def dynamic_programming(index: int) -> int:
        if index > 365:
            return 0

        if index not in days_set:
            return dynamic_programming(index + 1)

        return min(
            costs[0] + dynamic_programming(index + 1),
            costs[1] + dynamic_programming(index + 7),
            costs[2] + dynamic_programming(index + 30),
        )

    return dynamic_programming(1)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Clamp or split data to a single non-leap year: filter days to 1..365 and handle the remainder separately.
  2. If processing a leap year, map day 366 to 365 or treat the year as two calls.
  3. Validate before calling: if max(days) >= 366: raise/split the input.

Example fix

# before
mincost_tickets([1, 200, 366], [2, 7, 15])  # ValueError

# after
mincost_tickets([d for d in [1, 200, 366] if d <= 365], [2, 7, 15])
Defensive patterns

Strategy: validation

Validate before calling

def valid_days(days: list[int]) -> bool:
    return len(days) > 0 and max(days) <= 365 and min(days) >= 1

Try / catch

try:
    mincost_tickets(days, costs)
except ValueError as e:
    if 'less than 366' in str(e):
        days = [d for d in days if d <= 365]  # or split the year
    else:
        raise

Prevention

When it happens

Trigger: Calling mincost_tickets([200, 366], [2, 7, 15]) or with day 365+1 from date arithmetic. Any max(days) >= 366 triggers it, including day 400 or 1000.

Common situations: Using day-of-year values from a leap year (Dec 31 = day 366); passing raw day-of-year from datetime.timetuple().tm_ydata for a leap year; accidentally passing a day count across two years.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/45c77798c753ce5f. Report an issue: GitHub.