|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Python Bisect Module\n", |
| 8 | + "Used to find the insertion point for adding an item to a sorted list. \n", |
| 9 | + "Advantage: it's fast. Runs in O(log n). \n", |
| 10 | + "[Documentation](https://docs.python.org/3/library/bisect.html)" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 1, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "import bisect" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "markdown", |
| 24 | + "metadata": {}, |
| 25 | + "source": [ |
| 26 | + "### bisect_left\n", |
| 27 | + "Finds the insertion point for an item in a sorted list, or the spot just left of any matches. \n", |
| 28 | + "Works for list of ints, list of floats, list of strings." |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 2, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [ |
| 36 | + { |
| 37 | + "name": "stdout", |
| 38 | + "output_type": "stream", |
| 39 | + "text": [ |
| 40 | + "2\n", |
| 41 | + "3\n", |
| 42 | + "2\n" |
| 43 | + ] |
| 44 | + } |
| 45 | + ], |
| 46 | + "source": [ |
| 47 | + "a = [24, 33, 41, 41, 45, 50, 53, 59, 62, 66, 70]\n", |
| 48 | + "i = bisect.bisect_left(a, 41)\n", |
| 49 | + "print(i)\n", |
| 50 | + "\n", |
| 51 | + "b = [1.3, 2.2, 3.4, 4.6, 5.5, 6.9, 7.2, 8.4]\n", |
| 52 | + "j = bisect.bisect_left(b, 4.1)\n", |
| 53 | + "print(j)\n", |
| 54 | + "\n", |
| 55 | + "c = ['aaa', 'bbb', 'ccc', 'ddd']\n", |
| 56 | + "k = bisect.bisect_left(c, 'bug')\n", |
| 57 | + "print(k)" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "If list is unsorted, results are unpredictable, but it still tries." |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": 3, |
| 70 | + "metadata": {}, |
| 71 | + "outputs": [ |
| 72 | + { |
| 73 | + "name": "stdout", |
| 74 | + "output_type": "stream", |
| 75 | + "text": [ |
| 76 | + "2\n" |
| 77 | + ] |
| 78 | + } |
| 79 | + ], |
| 80 | + "source": [ |
| 81 | + "a = [33, 24, 41, 41, 45, 50, 53, 59, 66, 62, 70]\n", |
| 82 | + "i = bisect.bisect_left(a, 30)\n", |
| 83 | + "print(i)" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "markdown", |
| 88 | + "metadata": {}, |
| 89 | + "source": [ |
| 90 | + "### insort_left\n", |
| 91 | + "This inserts an item into the list in the correct position." |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": 4, |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [ |
| 99 | + { |
| 100 | + "name": "stdout", |
| 101 | + "output_type": "stream", |
| 102 | + "text": [ |
| 103 | + "[24, 33, 41, 41, 44, 45, 50, 53, 59, 62, 66, 70]\n" |
| 104 | + ] |
| 105 | + } |
| 106 | + ], |
| 107 | + "source": [ |
| 108 | + "d = [24, 33, 41, 41, 45, 50, 53, 59, 62, 66, 70]\n", |
| 109 | + "bisect.insort_left(d, 44)\n", |
| 110 | + "print(d)" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "markdown", |
| 115 | + "metadata": {}, |
| 116 | + "source": [ |
| 117 | + "### bisect_right\n", |
| 118 | + "Just like bisect_left, but for matches it returns the spot just to the right of matches." |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": 5, |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [ |
| 126 | + { |
| 127 | + "name": "stdout", |
| 128 | + "output_type": "stream", |
| 129 | + "text": [ |
| 130 | + "4\n", |
| 131 | + "2\n", |
| 132 | + "3\n" |
| 133 | + ] |
| 134 | + } |
| 135 | + ], |
| 136 | + "source": [ |
| 137 | + "a = [24, 33, 41, 41, 45, 50, 53, 59, 62, 66, 70]\n", |
| 138 | + "i = bisect.bisect_right(a, 41)\n", |
| 139 | + "print(i)\n", |
| 140 | + "\n", |
| 141 | + "b = [1.3, 2.2, 3.4, 4.6, 5.5, 6.9, 7.2, 8.4]\n", |
| 142 | + "j = bisect.bisect_right(b, 2.2)\n", |
| 143 | + "print(j)\n", |
| 144 | + "\n", |
| 145 | + "c = ['A', 'big', 'dog', 'runs', 'slowly']\n", |
| 146 | + "k = bisect.bisect_right(c, 'dog')\n", |
| 147 | + "print(k)" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "markdown", |
| 152 | + "metadata": {}, |
| 153 | + "source": [ |
| 154 | + "### insort_right\n", |
| 155 | + "Just like insort_left, but for matches it inserts to the right of the match." |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "code", |
| 160 | + "execution_count": 6, |
| 161 | + "metadata": {}, |
| 162 | + "outputs": [ |
| 163 | + { |
| 164 | + "name": "stdout", |
| 165 | + "output_type": "stream", |
| 166 | + "text": [ |
| 167 | + "[24, 33, 41, 41, 45, 46, 50, 53, 59, 62, 66, 70]\n" |
| 168 | + ] |
| 169 | + } |
| 170 | + ], |
| 171 | + "source": [ |
| 172 | + "d = [24, 33, 41, 41, 45, 50, 53, 59, 62, 66, 70]\n", |
| 173 | + "bisect.insort_right(d, 46)\n", |
| 174 | + "print(d)" |
| 175 | + ] |
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "markdown", |
| 179 | + "metadata": {}, |
| 180 | + "source": [ |
| 181 | + "### A fast Find function for a Sorted List\n", |
| 182 | + "Find leftmost value greater than x in sorted list a" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "code", |
| 187 | + "execution_count": 8, |
| 188 | + "metadata": {}, |
| 189 | + "outputs": [ |
| 190 | + { |
| 191 | + "name": "stdout", |
| 192 | + "output_type": "stream", |
| 193 | + "text": [ |
| 194 | + "False\n" |
| 195 | + ] |
| 196 | + } |
| 197 | + ], |
| 198 | + "source": [ |
| 199 | + "def find_next(a, x):\n", |
| 200 | + " i = bisect.bisect_right(a, x)\n", |
| 201 | + " if i < len(a):\n", |
| 202 | + " return a[i]\n", |
| 203 | + " return False\n", |
| 204 | + "\n", |
| 205 | + "print(find_next([10, 15, 20, 25, 30], 33))" |
| 206 | + ] |
| 207 | + }, |
| 208 | + { |
| 209 | + "cell_type": "markdown", |
| 210 | + "metadata": {}, |
| 211 | + "source": [ |
| 212 | + "### A simple get_grade function\n", |
| 213 | + "get_grade uses a list of cutoffs to split grades into 5 ranges, then uses the bisect index to return the corresponding grade. " |
| 214 | + ] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "code", |
| 218 | + "execution_count": 9, |
| 219 | + "metadata": {}, |
| 220 | + "outputs": [ |
| 221 | + { |
| 222 | + "name": "stdout", |
| 223 | + "output_type": "stream", |
| 224 | + "text": [ |
| 225 | + "['F', 'A', 'C', 'C', 'B', 'A', 'A']\n" |
| 226 | + ] |
| 227 | + } |
| 228 | + ], |
| 229 | + "source": [ |
| 230 | + "def get_grade(score, cutoffs=[60, 70, 80, 90], grades='FDCBA'):\n", |
| 231 | + " i = bisect.bisect_right(cutoffs, score)\n", |
| 232 | + " return grades[i]\n", |
| 233 | + "\n", |
| 234 | + "grades = [get_grade(score) for score in [52, 99, 77, 70, 89, 90, 100]]\n", |
| 235 | + "print(grades)" |
| 236 | + ] |
| 237 | + }, |
| 238 | + { |
| 239 | + "cell_type": "code", |
| 240 | + "execution_count": null, |
| 241 | + "metadata": {}, |
| 242 | + "outputs": [], |
| 243 | + "source": [] |
| 244 | + } |
| 245 | + ], |
| 246 | + "metadata": { |
| 247 | + "kernelspec": { |
| 248 | + "display_name": "Python 3", |
| 249 | + "language": "python", |
| 250 | + "name": "python3" |
| 251 | + }, |
| 252 | + "language_info": { |
| 253 | + "codemirror_mode": { |
| 254 | + "name": "ipython", |
| 255 | + "version": 3 |
| 256 | + }, |
| 257 | + "file_extension": ".py", |
| 258 | + "mimetype": "text/x-python", |
| 259 | + "name": "python", |
| 260 | + "nbconvert_exporter": "python", |
| 261 | + "pygments_lexer": "ipython3", |
| 262 | + "version": "3.7.0" |
| 263 | + } |
| 264 | + }, |
| 265 | + "nbformat": 4, |
| 266 | + "nbformat_minor": 2 |
| 267 | +} |
0 commit comments