TheAlgorithms/Python · error · ValueError

All days elements should be greater than 0

Error message

All days elements should be greater than 0

What it means

Raised by mincost_tickets() when any travel day is <= 0. The algorithm indexes a DP over day numbers 1..365 (dynamic_programming(index) recurses up to 365 then stops), so days must be positive calendar days. Zero or negative values would corrupt the recursion bounds and are rejected after the empty-list check.

Source

Thrown at dynamic_programming/minimum_tickets_cost.py:102

    >>> mincost_tickets([2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [1, 2, 3, 4])
    Traceback (most recent call last):
     ...
    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),

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert 0-based day offsets to 1-based by adding 1: days = [d + 1 for d in days].
  2. Sanitize input dates before calling: reject or remap any day <= 0.
  3. If days represent elapsed days from a start date, recompute them as actual calendar day numbers (1..365).

Example fix

# before
mincost_tickets([0, 3, 7], [2, 7, 15])  # ValueError

# after
mincost_tickets([1, 4, 8], [2, 7, 15])
Defensive patterns

Strategy: validation

Validate before calling

def valid_days(days: list[int]) -> bool:
    return all(isinstance(d, int) and 1 <= d <= 365 for d in days)

Type guard

def is_valid_day_list(days: object) -> TypeGuard[list[int]]:
    return isinstance(days, list) and all(
        type(d) is int and 1 <= d <= 365 for d in days
    )

Try / catch

try:
    mincost_tickets(days, costs)
except ValueError as e:
    if 'greater than 0' in str(e):
        days = [d + 1 for d in days]  # fix 0-based offsets
    else:
        raise

Prevention

When it happens

Trigger: Calling mincost_tickets([0, 5, 10], [2, 7, 15]), or with negative days such as mincost_tickets([-3, 40], [2, 7, 15]). Only triggers when days is non-empty, since len(days) == 0 returns 0 first.

Common situations: Using 0-based day offsets instead of 1-based calendar days (day 0 meaning 'today'); off-by-one bugs when computing day numbers from timestamps; feeding parsed dates that failed and defaulted to 0.

Related errors


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