interactive-coding-challenges/graphs_trees/min_heap/min_heap_challenge.ipynb
2017-03-30 05:39:53 -04:00

215 lines
5.1 KiB
Python

{
"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 min heap with extract_min and insert methods.\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",
"* Can we assume the inputs are ints?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* Extract min of an empty tree\n",
"* Extract min general case\n",
"* Insert into an empty tree\n",
"* Insert general case (left and right insert)\n",
"\n",
"<pre>\n",
" _5_\n",
" / \\\n",
" 20 15\n",
" / \\ / \\\n",
" 22 40 25\n",
" \n",
"extract_min(): 5\n",
"\n",
" _15_\n",
" / \\\n",
" 20 25\n",
" / \\ / \\\n",
" 22 40 \n",
"\n",
"insert(2):\n",
"\n",
" _2_\n",
" / \\\n",
" 20 5\n",
" / \\ / \\\n",
" 22 40 25 15\n",
"</pre>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/min_heap/min_heap_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": false
},
"outputs": [],
"source": [
"class MinHeap(object):\n",
"\n",
" def __init__(self):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def extract_min(self):\n",
" # TODO: Implement me\n",
" pass \n",
"\n",
" def peek_min(self):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def insert(self, data):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def _bubble_up(self, index):\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": {
"collapsed": false
},
"outputs": [],
"source": [
"# %load test_min_heap.py\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class TestMinHeap(object):\n",
"\n",
" def test_min_heap(self):\n",
" heap = MinHeap()\n",
" assert_equal(heap.peek_min(), None)\n",
" assert_equal(heap.extract_min(), None)\n",
" heap.insert(20)\n",
" assert_equal(heap.peek_min(), 20)\n",
" heap.insert(5)\n",
" assert_equal(heap.peek_min(), 5)\n",
" heap.insert(15)\n",
" heap.insert(22)\n",
" heap.insert(40)\n",
" heap.insert(5)\n",
" assert_equal(heap.peek_min(), 5)\n",
" heap.insert(3)\n",
" assert_equal(heap.peek_min(), 3)\n",
" assert_equal(heap.extract_min(), 3)\n",
" assert_equal(heap.peek_min(), 5)\n",
" print('Success: test_min_heap')\n",
"\n",
" \n",
"def main():\n",
" test = TestMinHeap()\n",
" test.test_min_heap()\n",
"\n",
" \n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/min_heap/min_heap_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.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}