donnemartin/interactive-coding-challenges · error · TypeError
rows and cols cannot be None
Error message
rows and cols cannot be None
What it means
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.
Source
Thrown at online_judges/sentence_screen_fit/sentence_screen_fit_solution.ipynb:165
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def count_sentence_fit_brute_force(self, sentence, rows, cols):\n",
" if sentence is None:\n",
" raise TypeError('sentence cannot be None')\n",
" if rows is None or cols is None:\n",
" raise TypeError('rows and cols cannot be None')\n",
" if rows < 0 or cols < 0:\n",
" raise ValueError('rows and cols cannot be negative')\n",
" if cols == 0 or not sentence:\n",
" return 0\n",
" curr_row = 0\n",
" curr_col = 0\n",
" count = 0\n",
" while curr_row < cols:\n",
" for word in sentence:\n",
" # If the current word doesn't fit on the current line,\n",
" # move to the next line\n",
" if len(word) > cols - curr_col:\n",
" curr_col = 0\n",
" curr_row += 1\n",
" # If we are beyond the number of rows, return\n",
" if curr_row >= rows:\n",
" return count\n",
" # If the current word fits on the current line,\n",View on GitHub (pinned to 358f2cc604)
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
Example fix
// before sol.count_sentence_fit_brute_force(['hi'], None, None) // after sol.count_sentence_fit_brute_force(['hi'], rows=3, cols=8)
Defensive patterns
Strategy: type-guard
Validate before calling
assert isinstance(rows, int) and isinstance(cols, int), 'rows and cols must be ints' sol.count_sentence_fit_brute_force(sentence, rows, cols)
Type guard
def valid_dims(rows, cols):
return isinstance(rows, int) and isinstance(cols, int) and not isinstance(rows, bool) and not isinstance(cols, bool) Try / catch
try:
sol.count_sentence_fit_brute_force(sentence, rows, cols)
except TypeError as e:
if 'rows and cols cannot be None' in str(e):
rows, cols = 0, 0 # sensible no-screen default
else:
raise Prevention
- Avoid None defaults for dimension parameters
- Validate parsed config dimensions once at load time
- Use keyword arguments (rows=..., cols=...) to prevent positional mix-ups
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- sentence cannot be None
- prices must have at least two values
- array cannot be None
- array must have 3 or more ints
- rows and cols cannot be negative
AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28).
Data as JSON: /api/errors/8b5919182c7cf5c7.
Report an issue: GitHub.