donnemartin/interactive-coding-challenges · error · TypeError
matrices cannot be None
Error message
matrices cannot be None
What it means
Raised by MatrixMultiplicationCost.find_min_cost when matrices is None. The method builds a square DP table of size len(matrices) and reads matrix dimensions from each entry, so a None list is rejected explicitly before T is allocated.
Source
Thrown at recursion_dynamic/matrix_mult/find_min_cost_solution.ipynb:200
" def __init__(self, first, second):\n",
" self.first = first\n",
" self.second = second"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"\n",
"class MatrixMultiplicationCost(object):\n",
"\n",
" def find_min_cost(self, matrices):\n",
" if matrices is None:\n",
" raise TypeError('matrices cannot be None')\n",
" if not matrices:\n",
" return 0\n",
" size = len(matrices)\n",
" T = [[0] * size for _ in range(size)]\n",
" for offset in range(1, size):\n",
" for i in range(size-offset):\n",
" j = i + offset\n",
" min_cost = sys.maxsize\n",
" for k in range(i, j):\n",
" cost = (T[i][k] + T[k+1][j] +\n",
" matrices[i].first *\n",
" matrices[k].second *\n",
" matrices[j].second)\n",
" if cost < min_cost:\n",
" min_cost = cost\n",
" T[i][j] = min_cost\n",
" return T[0][size-1]"
]View on GitHub (pinned to 358f2cc604)
Solutions
- Pass a list of matrix dimension tuples (e.g. [(2,3),(3,6)]) and default to [] when absent
- Fix the parser to return [] on failure instead of None
- Guard the call site with 'if matrices is not None'
Example fix
// before cost = mmc.find_min_cost(parse_dims(text)) // after dims = parse_dims(text) or [] cost = mmc.find_min_cost(dims)
Defensive patterns
Strategy: validation
Validate before calling
matrices = matrices or [] mmc.find_min_cost(matrices)
Type guard
def is_dim_list(x):
return isinstance(x, list) and all(isinstance(m, tuple) and len(m) == 2 for m in x) Try / catch
try:
mmc.find_min_cost(matrices)
except TypeError:
cost = 0 Prevention
- Parsers should return [] on failure
- Validate dimension tuples at parse time
When it happens
Trigger: Calling find_min_cost(None). An empty list is valid and returns 0; only None raises the TypeError.
Common situations: Matrix dimension chains parsed from input that may be absent; a chain-building helper returning None on parse failure; passing an uninitialized variable.
Related errors
- coins or total cannot be None
- input_items or total_weight cannot be None
- items or total_weight cannot be None
- str input cannot be None
- seq cannot be None
AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28).
Data as JSON: /api/errors/6ee6092e5aecf5ce.
Report an issue: GitHub.