Skip to content

Commit

Permalink
Merge pull request #153 from donnemartin/develop
Browse files Browse the repository at this point in the history
Add 64 new challenges
  • Loading branch information
donnemartin authored Apr 3, 2017
2 parents 70414a6 + 68d2a47 commit 763fe6b
Show file tree
Hide file tree
Showing 262 changed files with 30,911 additions and 51 deletions.
235 changes: 190 additions & 45 deletions README.md

Large diffs are not rendered by default.

Binary file added anki_cards/Coding.apkg
Binary file not shown.
Empty file.
41 changes: 41 additions & 0 deletions arrays_strings/priority_queue/priority_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sys


class PriorityQueueNode(object):

def __init__(self, obj, key):
self.obj = obj
self.key = key

def __repr__(self):
return str(self.obj) + ': ' + str(self.key)


class PriorityQueue(object):

def __init__(self):
self.array = []

def __len__(self):
return len(self.array)

def insert(self, node):
self.array.append(node)
return self.array[-1]

def extract_min(self):
if not self.array:
return None
minimum = sys.maxsize
for index, node in enumerate(self.array):
if node.key < minimum:
minimum = node.key
minimum_index = index
return self.array.pop(minimum_index)

def decrease_key(self, obj, new_key):
for node in self.array:
if node.obj is obj:
node.key = new_key
return node
return None
209 changes: 209 additions & 0 deletions arrays_strings/priority_queue/priority_queue_challenge.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
{
"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](https://github.com/donnemartin/interactive-coding-challenges/arrays_strings/priority_queue/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": {
"collapsed": false
},
"outputs": [],
"source": [
"# %load test_priority_queue.py\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class TestPriorityQueue(object):\n",
"\n",
" def test_priority_queue(self):\n",
" priority_queue = PriorityQueue()\n",
" assert_equal(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",
" assert_equal(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](https://github.com/donnemartin/interactive-coding-challenges/arrays_strings/priority_queue/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.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading

0 comments on commit 763fe6b

Please sign in to comment.