{"record":{"id":"8af19dd10a01e50a","repo":"donnemartin/interactive-coding-challenges","slug":"prices-or-k-cannot-be-none","errorCode":null,"errorMessage":"prices or k cannot be None","messagePattern":"prices or k cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"recursion_dynamic/max_profit_k/max_profit_solution.ipynb","lineNumber":168,"sourceCode":"    \"        return str(self.type) + ' day: ' + \\\\\\n\",\n    \"            str(self.day) + ' price: ' + \\\\\\n\",\n    \"            str(self.price)\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 2,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"import sys\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"class StockTrader(object):\\n\",\n    \"\\n\",\n    \"    def find_max_profit(self, prices, k):\\n\",\n    \"        if prices is None or k is None:\\n\",\n    \"            raise TypeError('prices or k cannot be None')\\n\",\n    \"        if not prices or k <= 0:\\n\",\n    \"            return []\\n\",\n    \"        num_rows = k + 1  # 0th transaction for dp table\\n\",\n    \"        num_cols = len(prices)\\n\",\n    \"        T = [[None] * num_cols for _ in range(num_rows)]\\n\",\n    \"        for i in range(num_rows):\\n\",\n    \"            for j in range(num_cols):\\n\",\n    \"                if i == 0 or j == 0:\\n\",\n    \"                    T[i][j] = 0\\n\",\n    \"                    continue\\n\",\n    \"                max_profit = -sys.maxsize\\n\",\n    \"                for m in range(j):\\n\",\n    \"                    profit = prices[j] - prices[m] + T[i - 1][m]\\n\",\n    \"                    if profit > max_profit:\\n\",\n    \"                        max_profit = profit\\n\",\n    \"                T[i][j] = max(T[i][j - 1], max_profit)\\n\",\n    \"        return self._find_max_profit_transactions(T, prices)\\n\",\n    \"\\n\",","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/recursion_dynamic/max_profit_k/max_profit_solution.ipynb#L150-L186","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ntrades = trader.find_max_profit(fetch_prices(sym), k=None)\n// after\nprices = fetch_prices(sym)\nif prices is None:\n    prices = []\ntrades = trader.find_max_profit(prices, k=2)","handlingStrategy":"validation","validationCode":"if prices is None or k is None:\n    return []\ntrader.find_max_profit(prices, k)","typeGuard":"def valid_trader_args(prices, k):\n    return isinstance(prices, list) and isinstance(k, int) and k > 0","tryCatchPattern":"try:\n    trader.find_max_profit(prices, k)\nexcept TypeError:\n    trades = []","preventionTips":["Check fetch results before computing","Default k from config as int"],"tags":["python","dynamic-programming","stocks","input-validation"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}