{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Bisect Module\n", "Used to find the insertion point for adding an item to a sorted list. \n", "Advantage: it's fast. Runs in O(log n). \n", "[Documentation](https://docs.python.org/3/library/bisect.html)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import bisect" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### bisect_left\n", "Finds the insertion point for an item in a sorted list, or the spot just left of any matches. \n", "Works for list of ints, list of floats, list of strings." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n", "2\n" ] } ], "source": [ "a = [24, 33, 41, 41, 45, 50, 53, 59, 62, 66, 70]\n", "i = bisect.bisect_left(a, 41)\n", "print(i)\n", "\n", "b = [1.3, 2.2, 3.4, 4.6, 5.5, 6.9, 7.2, 8.4]\n", "j = bisect.bisect_left(b, 4.1)\n", "print(j)\n", "\n", "c = ['aaa', 'bbb', 'ccc', 'ddd']\n", "k = bisect.bisect_left(c, 'bug')\n", "print(k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If list is unsorted, results are unpredictable, but it still tries." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "a = [33, 24, 41, 41, 45, 50, 53, 59, 66, 62, 70]\n", "i = bisect.bisect_left(a, 30)\n", "print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### insort_left\n", "This inserts an item into the list in the correct position." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[24, 33, 41, 41, 44, 45, 50, 53, 59, 62, 66, 70]\n" ] } ], "source": [ "d = [24, 33, 41, 41, 45, 50, 53, 59, 62, 66, 70]\n", "bisect.insort_left(d, 44)\n", "print(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### bisect_right\n", "Just like bisect_left, but for matches it returns the spot just to the right of matches." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "2\n", "3\n" ] } ], "source": [ "a = [24, 33, 41, 41, 45, 50, 53, 59, 62, 66, 70]\n", "i = bisect.bisect_right(a, 41)\n", "print(i)\n", "\n", "b = [1.3, 2.2, 3.4, 4.6, 5.5, 6.9, 7.2, 8.4]\n", "j = bisect.bisect_right(b, 2.2)\n", "print(j)\n", "\n", "c = ['A', 'big', 'dog', 'runs', 'slowly']\n", "k = bisect.bisect_right(c, 'dog')\n", "print(k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### insort_right\n", "Just like insort_left, but for matches it inserts to the right of the match." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[24, 33, 41, 41, 45, 46, 50, 53, 59, 62, 66, 70]\n" ] } ], "source": [ "d = [24, 33, 41, 41, 45, 50, 53, 59, 62, 66, 70]\n", "bisect.insort_right(d, 46)\n", "print(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A fast Find function for a Sorted List\n", "Find leftmost value greater than x in sorted list a" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "def find_next(a, x):\n", " i = bisect.bisect_right(a, x)\n", " if i < len(a):\n", " return a[i]\n", " return False\n", "\n", "print(find_next([10, 15, 20, 25, 30], 33))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A simple get_grade function\n", "get_grade uses a list of cutoffs to split grades into 5 ranges, then uses the bisect index to return the corresponding grade. " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['F', 'A', 'C', 'C', 'B', 'A', 'A']\n" ] } ], "source": [ "def get_grade(score, cutoffs=[60, 70, 80, 90], grades='FDCBA'):\n", " i = bisect.bisect_right(cutoffs, score)\n", " return grades[i]\n", "\n", "grades = [get_grade(score) for score in [52, 99, 77, 70, 89, 90, 100]]\n", "print(grades)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "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.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }