{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Challenge Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem: Implement a priority queue backed by an array.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", "* [Algorithm](#Algorithm)\n", "* [Code](#Code)\n", "* [Unit Test](#Unit-Test)\n", "* [Solution Notebook](#Solution-Notebook)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Constraints\n", "\n", "* Do we expect the methods to be insert, extract_min, and decrease_key?\n", " * Yes\n", "* Can we assume there aren't any duplicate keys?\n", " * Yes\n", "* Do we need to validate inputs?\n", " * No\n", "* Can we assume this fits memory?\n", " * Yes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Cases\n", "\n", "### insert\n", "\n", "* `insert` general case -> inserted node\n", "\n", "### extract_min\n", "\n", "* `extract_min` from an empty list -> None\n", "* `extract_min` general case -> min node\n", "\n", "### decrease_key\n", "\n", "* `decrease_key` an invalid key -> None\n", "* `decrease_key` general case -> updated node" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Algorithm\n", "\n", "Refer to the [Solution Notebook](priority_queue_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class PriorityQueueNode(object):\n", "\n", " def __init__(self, obj, key):\n", " self.obj = obj\n", " self.key = key\n", "\n", " def __repr__(self):\n", " return str(self.obj) + ': ' + str(self.key)\n", "\n", "\n", "class PriorityQueue(object):\n", "\n", " def __init__(self):\n", " self.array = []\n", "\n", " def __len__(self):\n", " return len(self.array)\n", "\n", " def insert(self, node):\n", " # TODO: Implement me\n", " pass\n", "\n", " def extract_min(self):\n", " # TODO: Implement me\n", " pass\n", "\n", " def decrease_key(self, obj, new_key):\n", " # TODO: Implement me\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Unit Test" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**The following unit test is expected to fail until you solve the challenge.**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %load test_priority_queue.py\n", "import unittest\n", "\n", "\n", "class TestPriorityQueue(unittest.TestCase):\n", "\n", " def test_priority_queue(self):\n", " priority_queue = PriorityQueue()\n", " self.assertEqual(priority_queue.extract_min(), None)\n", " priority_queue.insert(PriorityQueueNode('a', 20))\n", " priority_queue.insert(PriorityQueueNode('b', 5))\n", " priority_queue.insert(PriorityQueueNode('c', 15))\n", " priority_queue.insert(PriorityQueueNode('d', 22))\n", " priority_queue.insert(PriorityQueueNode('e', 40))\n", " priority_queue.insert(PriorityQueueNode('f', 3))\n", " priority_queue.decrease_key('f', 2)\n", " priority_queue.decrease_key('a', 19)\n", " mins = []\n", " while priority_queue.array:\n", " mins.append(priority_queue.extract_min().key)\n", " self.assertEqual(mins, [2, 5, 15, 19, 22, 40])\n", " print('Success: test_min_heap')\n", "\n", "\n", "def main():\n", " test = TestPriorityQueue()\n", " test.test_priority_queue()\n", "\n", "\n", "if __name__ == '__main__':\n", " main()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution Notebook\n", "\n", "Review the [Solution Notebook](priority_queue_solution.ipynb) for a discussion on algorithms and code solutions." ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 1 }