Added bisect snippets.

This commit is contained in:
Donne Martin 2015-01-25 08:25:44 -05:00
parent bf990b4beb
commit ff31684ee5

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:14f0a7907fbd8cdf35645b7ffb0656b900a879fbeff7073ce5c2bb91e932d356"
"signature": "sha256:d7e86a84d9ac272fcd99e1aadbc8c73b80d1affd3c0533649f6842229dee5f2b"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -525,13 +525,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 25,
"prompt_number": 23,
"text": [
"[1, 3, 5, 6, 7, 9]"
]
}
],
"prompt_number": 25
"prompt_number": 23
},
{
"cell_type": "code",
@ -548,13 +548,70 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 26,
"prompt_number": 24,
"text": [
"['the', 'fox', 'over', 'quick', 'brown', 'jumps']"
]
}
],
"prompt_number": 26
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## bisect"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# The bisect module does not check whether the list is sorted, as this check\n",
"# would be expensive O(n). Using bisect on an unsorted list will not result\n",
"# in an error but could lead to incorrect results.\n",
"import bisect\n",
"\n",
"# Find the location where an element should be inserted to keep the\n",
"# list sorted\n",
"c_list = [1, 2, 2, 4, 8, 10]\n",
"bisect.bisect(c_list, 5)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 40,
"text": [
"4"
]
}
],
"prompt_number": 40
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Inserts an element into a location to keep the list sorted\n",
"bisect.insort(c_list, 5)\n",
"c_list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 41,
"text": [
"[1, 2, 2, 4, 5, 8, 10]"
]
}
],
"prompt_number": 41
},
{
"cell_type": "code",