\u001b[0m in \u001b[0;36mtest_range_overlap\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mtest_range_overlap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mrange_overlap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m0.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m5.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m6.0\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mrange_overlap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m0.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m1.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2.0\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mrange_overlap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m0.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1.0\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m0.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mrange_overlap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m2.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m2.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4.0\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m2.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"test_range_overlap()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first of the tests that was supposed to produce `None` fails, so we know there's something wrong with our function. What we *don't* know, though, is whether the other four tests passed or failed, because Python halted the program as soon as it spotted the first error. \n",
"\n",
"Still, some information is better than none, and if we trace the behavior of the function with that input, we realize that we're initializing `lowest` and `highest` to 0.0 and 1.0 respectively, regardless of the input values."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" \n",
"\n",
"
Pre and post-conditions
\n",
"\n",
"\n",
"\n",
"> Suppose you are writing a function called `average` that calculates the average of the numbers in a list.\n",
"> What pre-conditions and post-conditions would you write for it?\n",
"> Compare your answer to your neighbor's:\n",
"> can you think of a function that will pass your tests but not hers or vice versa?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" \n",
"\n",
"
Testing Assertions
\n",
"\n",
"\n",
"\n",
"> Given a sequence of values, the function `running` returns\n",
"> a list containing the running totals at each index.\n",
">\n",
"> ```python\n",
"> running([1, 2, 3, 4])\n",
"> ```\n",
">\n",
"> ```python\n",
"> [1, 3, 6, 10]\n",
"> ```\n",
">\n",
"> ```python\n",
"> running('abc')\n",
"> ```\n",
">\n",
"> ```python\n",
"> ['a', 'ab', 'abc']\n",
"> ```\n",
">\n",
"> Explain in words what the assertions in the `running` function below check, and for each one, give an example of input that will make that assertion fail."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"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"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" \n",
"\n",
"
Testing Assertions
\n",
"\n",
"\n",
"\n",
"> Fix `range_overlap`. Re-run `test_range_overlap` after each change you make."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "IPython (Python 3)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}