Suppose you are writing a function called average
that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not hers or vice versa?
Given a sequence of values, the function running
returns a list containing the running totals at each index.
running([1, 2, 3, 4])
\n",
"[1, 3, 6, 10]
\n",
"running('abc')
\n",
"['a', 'ab', 'abc']
\n",
"Explain in words what the assertions in this function check, and for each one, give an example of input that will make that assertion fail.
\n", "def running(values):\n",
" assert len(values) > 0\n",
" result = [values[0]]\n",
" for v in values[1:]:\n",
" assert result[-1] >= 0\n",
" result.append(result[-1] + v)\n",
" assert result[-1] >= result[0]\n",
" return result
\n",
"Fix range_overlap
. Re-run test_range_overlap
after each change you make.