TheAlgorithms/Python · error · ValueError

The parameter costs should be a list of three integers

Error message

The parameter costs should be a list of three integers

What it means

Raised by mincost_tickets() when the costs argument is not a list of exactly three integers. The function models LeetCode's 'Minimum Cost For Tickets' problem, where the three costs correspond to 1-day, 7-day, and 30-day pass prices. Any other length or non-int element (e.g. floats like 2.5) is rejected before the DP runs.

Source

Thrown at dynamic_programming/minimum_tickets_cost.py:96

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

    >>> mincost_tickets([], [])
    Traceback (most recent call last):
     ...
    ValueError: The parameter costs should be a list of three integers

    >>> 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:

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass exactly three integer costs in the order [1-day, 7-day, 30-day], e.g. mincost_tickets(days, [2, 7, 15]).
  2. If costs come from JSON/float sources, convert each element: [int(c) for c in costs] and verify len == 3 first.
  3. Add a pre-call assertion or guard: assert len(costs) == 3 and all(isinstance(c, int) for c in costs).

Example fix

# before
mincost_tickets([1, 4, 6, 7, 8, 20], [1.5, 7.0, 15.0])  # ValueError

# after
costs = [int(c) for c in [1.5, 7.0, 15.0]]
mincost_tickets([1, 4, 6, 7, 8, 20], costs)
Defensive patterns

Strategy: validation

Validate before calling

def valid_costs(costs) -> bool:
    return isinstance(costs, list) and len(costs) == 3 and all(
        isinstance(c, int) and not isinstance(c, bool) for c in costs
    )

Type guard

def is_cost_triple(costs: object) -> TypeGuard[list[int]]:
    return isinstance(costs, list) and len(costs) == 3 and all(
        type(c) is int for c in costs
    )

Try / catch

try:
    mincost_tickets(days, costs)
except ValueError as e:
    if 'three integers' in str(e):
        costs = [int(round(c)) for c in costs[:3]]
    else:
        raise

Prevention

When it happens

Trigger: Calling mincost_tickets(days, costs) with len(costs) != 3, e.g. mincost_tickets([1,2,3,4],[1,2,3,4]) (4 costs), or with float/string elements such as [1.5, 2, 3] or ['1','2','3']. Note bool passes isinstance(x, int) since bool subclasses int.

Common situations: Passing a generic price list from user input or a config file without slicing to three entries; loading costs from JSON where values deserialize as floats (e.g. [2.0, 7.0, 15.0]); misunderstanding the API as accepting arbitrary pass types.

Related errors


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