mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
211 lines
5.1 KiB
Python
211 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": {},
|
|
"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": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# %load test_min_heap.py\n",
|
|
"import unittest\n",
|
|
"\n",
|
|
"\n",
|
|
"class TestMinHeap(unittest.TestCase):\n",
|
|
"\n",
|
|
" def test_min_heap(self):\n",
|
|
" heap = MinHeap()\n",
|
|
" self.assertEqual(heap.peek_min(), None)\n",
|
|
" self.assertEqual(heap.extract_min(), None)\n",
|
|
" heap.insert(20)\n",
|
|
" self.assertEqual(heap.peek_min(), 20)\n",
|
|
" heap.insert(5)\n",
|
|
" self.assertEqual(heap.peek_min(), 5)\n",
|
|
" heap.insert(15)\n",
|
|
" heap.insert(22)\n",
|
|
" heap.insert(40)\n",
|
|
" heap.insert(5)\n",
|
|
" self.assertEqual(heap.peek_min(), 5)\n",
|
|
" heap.insert(3)\n",
|
|
" self.assertEqual(heap.peek_min(), 3)\n",
|
|
" self.assertEqual(heap.extract_min(), 3)\n",
|
|
" self.assertEqual(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.7.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 1
|
|
}
|