donnemartin/interactive-coding-challenges · error · TypeError
prices or k cannot be None
Error message
prices or k cannot be None
What it means
Raised by StockTrader.find_max_profit when prices or k is None. The method allocates a DP table with num_rows = k + 1 and num_cols = len(prices) on the next lines, so None inputs are rejected up front with a clear TypeError before table construction.
Source
Thrown at recursion_dynamic/max_profit_k/max_profit_solution.ipynb:168
" return str(self.type) + ' day: ' + \\\n",
" str(self.day) + ' price: ' + \\\n",
" str(self.price)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"\n",
"class StockTrader(object):\n",
"\n",
" def find_max_profit(self, prices, k):\n",
" if prices is None or k is None:\n",
" raise TypeError('prices or k cannot be None')\n",
" if not prices or k <= 0:\n",
" return []\n",
" num_rows = k + 1 # 0th transaction for dp table\n",
" num_cols = len(prices)\n",
" T = [[None] * num_cols for _ in range(num_rows)]\n",
" for i in range(num_rows):\n",
" for j in range(num_cols):\n",
" if i == 0 or j == 0:\n",
" T[i][j] = 0\n",
" continue\n",
" max_profit = -sys.maxsize\n",
" for m in range(j):\n",
" profit = prices[j] - prices[m] + T[i - 1][m]\n",
" if profit > max_profit:\n",
" max_profit = profit\n",
" T[i][j] = max(T[i][j - 1], max_profit)\n",
" return self._find_max_profit_transactions(T, prices)\n",
"\n",View on GitHub (pinned to 358f2cc604)
Solutions
- Pass a numeric price list and an int k, defaulting missing values (prices = prices or [], k = k or 0)
- Check the fetch result before computing: if prices is None: handle error
- Validate parsed market data shape at ingestion
Example fix
// before
trades = trader.find_max_profit(fetch_prices(sym), k=None)
// after
prices = fetch_prices(sym)
if prices is None:
prices = []
trades = trader.find_max_profit(prices, k=2) Defensive patterns
Strategy: validation
Validate before calling
if prices is None or k is None:
return []
trader.find_max_profit(prices, k) Type guard
def valid_trader_args(prices, k):
return isinstance(prices, list) and isinstance(k, int) and k > 0 Try / catch
try:
trader.find_max_profit(prices, k)
except TypeError:
trades = [] Prevention
- Check fetch results before computing
- Default k from config as int
When it happens
Trigger: Calling find_max_profit(None, 2) or find_max_profit(prices, None). Empty prices or k <= 0 returns [] without raising — only None triggers the error.
Common situations: Price series fetched from an API that returned an error body; k left as None from optional config; reusing a data loader that signals failure with None.
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/8af19dd10a01e50a.
Report an issue: GitHub.