{"record":{"id":"92ac8c099fa5298f","repo":"donnemartin/interactive-coding-challenges","slug":"input-items-or-total-weight-cannot-be-none","errorCode":null,"errorMessage":"input_items or total_weight cannot be None","messagePattern":"input_items or total_weight cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"recursion_dynamic/knapsack_01/knapsack_solution.ipynb","lineNumber":166,"sourceCode":"  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"### Knapsack Bottom Up\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 2,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class Knapsack(object):\\n\",\n    \"\\n\",\n    \"    def fill_knapsack(self, input_items, total_weight):\\n\",\n    \"        if input_items is None or total_weight is None:\\n\",\n    \"            raise TypeError('input_items or total_weight cannot be None')\\n\",\n    \"        if not input_items or total_weight == 0:\\n\",\n    \"            return 0\\n\",\n    \"        items = list([Item(label='', value=0, weight=0)] + input_items)\\n\",\n    \"        num_rows = len(items)\\n\",\n    \"        num_cols = total_weight + 1\\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    \"                elif j >= items[i].weight:\\n\",\n    \"                    T[i][j] = max(items[i].value + T[i - 1][j - items[i].weight],\\n\",\n    \"                                  T[i - 1][j])\\n\",\n    \"                else:\\n\",\n    \"                    T[i][j] = T[i - 1][j]\\n\",\n    \"        results = []\\n\",\n    \"        i = num_rows - 1\\n\",\n    \"        j = num_cols - 1\\n\",","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/recursion_dynamic/knapsack_01/knapsack_solution.ipynb#L148-L184","documentation":"Raised by Knapsack.fill_knapsack (bottom-up 0/1 knapsack) when input_items or total_weight is None. The dynamic-programming table construction immediately indexes items and uses total_weight as a column count, so None inputs are rejected up front with a TypeError rather than crashing inside the table loop.","triggerScenarios":"Calling fill_knapsack(None, 10) or fill_knapsack(items, None). Note the next line treats an empty list or total_weight == 0 as a valid 0-return case, so only None triggers the raise.","commonSituations":"Loading item lists from JSON/datasets where the items key is missing; passing an uninitialized weight; wiring the call to optional config values that default to None.","solutions":["Pass a real list of Item objects (even [] is fine and returns 0) and an integer total_weight","Default optional values: items = items or [] and weight = 0 if weight is None","Validate parsed input before constructing the Knapsack call"],"exampleFix":"// before\nks.fill_knapsack(None, total_weight=10)\n// after\nitems = items if items is not None else []\nks.fill_knapsack(items, 10)","handlingStrategy":"type-guard","validationCode":"assert input_items is not None and total_weight is not None\nks.fill_knapsack(input_items, total_weight)","typeGuard":"def valid_knapsack_input(items, w):\n    return isinstance(items, list) and isinstance(w, int) and w >= 0","tryCatchPattern":"try:\n    value = ks.fill_knapsack(items, w)\nexcept TypeError:\n    value = 0  # treat as empty instance","preventionTips":["Default items to [] and weight to 0 at boundaries","Ensure loaders never return None"],"tags":["python","dynamic-programming","knapsack","input-validation","typeerror"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}