{"record":{"id":"8b5919182c7cf5c7","repo":"donnemartin/interactive-coding-challenges","slug":"rows-and-cols-cannot-be-none","errorCode":null,"errorMessage":"rows and cols cannot be None","messagePattern":"rows and cols cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"online_judges/sentence_screen_fit/sentence_screen_fit_solution.ipynb","lineNumber":165,"sourceCode":"   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class Solution(object):\\n\",\n    \"\\n\",\n    \"    def count_sentence_fit_brute_force(self, sentence, rows, cols):\\n\",\n    \"        if sentence is None:\\n\",\n    \"            raise TypeError('sentence cannot be None')\\n\",\n    \"        if rows is None or cols is None:\\n\",\n    \"            raise TypeError('rows and cols cannot be None')\\n\",\n    \"        if rows < 0 or cols < 0:\\n\",\n    \"            raise ValueError('rows and cols cannot be negative')\\n\",\n    \"        if cols == 0 or not sentence:\\n\",\n    \"            return 0\\n\",\n    \"        curr_row = 0\\n\",\n    \"        curr_col = 0\\n\",\n    \"        count = 0\\n\",\n    \"        while curr_row < cols:\\n\",\n    \"            for word in sentence:\\n\",\n    \"                # If the current word doesn't fit on the current line,\\n\",\n    \"                # move to the next line\\n\",\n    \"                if len(word) > cols - curr_col:\\n\",\n    \"                    curr_col = 0\\n\",\n    \"                    curr_row += 1\\n\",\n    \"                # If we are beyond the number of rows, return\\n\",\n    \"                if curr_row >= rows:\\n\",\n    \"                    return count\\n\",\n    \"                # If the current word fits on the current line,\\n\",","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/online_judges/sentence_screen_fit/sentence_screen_fit_solution.ipynb#L147-L183","documentation":"Raised when rows or cols (or both) are None in the brute-force sentence screen fit solver. The method validates rows and cols immediately after the sentence check and rejects None because comparing None with 0 later would raise TypeError anyway. The guard makes the failure explicit and attributable at the API boundary.","triggerScenarios":"Calling count_sentence_fit_brute_force(sentence, None, 10) or (sentence, 5, None), typically when dimensions come from optional config or a function that returns None on failure to parse screen size.","commonSituations":"Reading screen dimensions from argparse/int() where the value is missing; a caller swapping argument order so None lands in rows/cols; defaults of None used as 'not specified' sentinels reaching the algorithm.","solutions":["Pass concrete non-negative integers for rows and cols","If dimensions are optional upstream, default them explicitly (rows = rows if rows is not None else 0) before calling","Validate parsed config values before invoking the solver"],"exampleFix":"// before\nsol.count_sentence_fit_brute_force(['hi'], None, None)\n// after\nsol.count_sentence_fit_brute_force(['hi'], rows=3, cols=8)","handlingStrategy":"type-guard","validationCode":"assert isinstance(rows, int) and isinstance(cols, int), 'rows and cols must be ints'\nsol.count_sentence_fit_brute_force(sentence, rows, cols)","typeGuard":"def valid_dims(rows, cols):\n    return isinstance(rows, int) and isinstance(cols, int) and not isinstance(rows, bool) and not isinstance(cols, bool)","tryCatchPattern":"try:\n    sol.count_sentence_fit_brute_force(sentence, rows, cols)\nexcept TypeError as e:\n    if 'rows and cols cannot be None' in str(e):\n        rows, cols = 0, 0  # sensible no-screen default\n    else:\n        raise","preventionTips":["Avoid None defaults for dimension parameters","Validate parsed config dimensions once at load time","Use keyword arguments (rows=..., cols=...) to prevent positional mix-ups"],"tags":["python","input-validation","typeerror","algorithm"],"backgroundTag":"none-argument-typeerror","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}