{"record":{"id":"c097fa9f40df98ad","repo":"donnemartin/interactive-coding-challenges","slug":"rows-and-cols-cannot-be-negative","errorCode":null,"errorMessage":"rows and cols cannot be negative","messagePattern":"rows and cols cannot be negative","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"online_judges/sentence_screen_fit/sentence_screen_fit_solution.ipynb","lineNumber":167,"sourceCode":"   \"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\",\n    \"                # 'insert' it here\\n\",\n    \"                if len(word) <= cols - curr_col:\\n\",","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/online_judges/sentence_screen_fit/sentence_screen_fit_solution.ipynb#L149-L185","documentation":"Raised when rows or cols is a negative number in the brute-force sentence screen fit solver. A screen cannot have negative dimensions, so the method rejects them with ValueError before running the fitting loop. This distinguishes 'invalid value' from 'wrong type' (None cases raise TypeError instead).","triggerScenarios":"Calling count_sentence_fit_brute_force(sentence, -1, 10) or (sentence, 5, -3); passing a computed dimension that underflowed to negative (e.g. cols = width - margin with margin > width).","commonSituations":"Subtraction-based size calculations that go negative for small screens; CSV/config values entered with a stray minus sign; fuzz tests that probe boundary integers.","solutions":["Clamp or reject negative dimensions before calling: cols = max(cols, 0)","Fix the upstream arithmetic that produced the negative value","Validate user-supplied dimensions against a minimum of 0 in the input layer"],"exampleFix":"// before\nsol.count_sentence_fit_brute_force(['hi'], rows=2, cols=width - 20)  # width=10 -> -10\n// after\nsol.count_sentence_fit_brute_force(['hi'], rows=2, cols=max(width - 20, 0))","handlingStrategy":"validation","validationCode":"rows = max(rows, 0)\ncols = max(cols, 0)\nsol.count_sentence_fit_brute_force(sentence, rows, cols)","typeGuard":"def nonneg_int(x):\n    return isinstance(x, int) and x >= 0","tryCatchPattern":"try:\n    sol.count_sentence_fit_brute_force(sentence, rows, cols)\nexcept ValueError as e:\n    if 'cannot be negative' in str(e):\n        raise ValueError(f'invalid screen dims: {rows}x{cols}') from e\n    raise","preventionTips":["Clamp computed dimensions with max(value, 0)","Sanity-check dimensions at the UI/config boundary","Distinguish TypeError (None) from ValueError (negative) when testing"],"tags":["python","input-validation","valueerror","algorithm"],"backgroundTag":"negative-dimension-valueerror","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}