donnemartin/interactive-coding-challenges · error · ValueError

rows and cols cannot be negative

Error message

rows and cols cannot be negative

What it means

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).

Source

Thrown at online_judges/sentence_screen_fit/sentence_screen_fit_solution.ipynb:167

   "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",
    "                # 'insert' it here\n",
    "                if len(word) <= cols - curr_col:\n",

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Clamp or reject negative dimensions before calling: cols = max(cols, 0)
  2. Fix the upstream arithmetic that produced the negative value
  3. Validate user-supplied dimensions against a minimum of 0 in the input layer

Example fix

// before
sol.count_sentence_fit_brute_force(['hi'], rows=2, cols=width - 20)  # width=10 -> -10
// after
sol.count_sentence_fit_brute_force(['hi'], rows=2, cols=max(width - 20, 0))
Defensive patterns

Strategy: validation

Validate before calling

rows = max(rows, 0)
cols = max(cols, 0)
sol.count_sentence_fit_brute_force(sentence, rows, cols)

Type guard

def nonneg_int(x):
    return isinstance(x, int) and x >= 0

Try / catch

try:
    sol.count_sentence_fit_brute_force(sentence, rows, cols)
except ValueError as e:
    if 'cannot be negative' in str(e):
        raise ValueError(f'invalid screen dims: {rows}x{cols}') from e
    raise

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28). Data as JSON: /api/errors/c097fa9f40df98ad. Report an issue: GitHub.