Skip to content

Commit 763fe6b

Browse files
authored
Merge pull request donnemartin#153 from donnemartin/develop
Add 64 new challenges
2 parents 70414a6 + 68d2a47 commit 763fe6b

File tree

262 files changed

+30911
-51
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

262 files changed

+30911
-51
lines changed

README.md

Lines changed: 190 additions & 45 deletions
Large diffs are not rendered by default.

anki_cards/Coding.apkg

638 KB
Binary file not shown.

arrays_strings/priority_queue/__init__.py

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import sys
2+
3+
4+
class PriorityQueueNode(object):
5+
6+
def __init__(self, obj, key):
7+
self.obj = obj
8+
self.key = key
9+
10+
def __repr__(self):
11+
return str(self.obj) + ': ' + str(self.key)
12+
13+
14+
class PriorityQueue(object):
15+
16+
def __init__(self):
17+
self.array = []
18+
19+
def __len__(self):
20+
return len(self.array)
21+
22+
def insert(self, node):
23+
self.array.append(node)
24+
return self.array[-1]
25+
26+
def extract_min(self):
27+
if not self.array:
28+
return None
29+
minimum = sys.maxsize
30+
for index, node in enumerate(self.array):
31+
if node.key < minimum:
32+
minimum = node.key
33+
minimum_index = index
34+
return self.array.pop(minimum_index)
35+
36+
def decrease_key(self, obj, new_key):
37+
for node in self.array:
38+
if node.obj is obj:
39+
node.key = new_key
40+
return node
41+
return None
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"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)."
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# Challenge Notebook"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"## Problem: Implement a priority queue backed by an array.\n",
22+
"\n",
23+
"* [Constraints](#Constraints)\n",
24+
"* [Test Cases](#Test-Cases)\n",
25+
"* [Algorithm](#Algorithm)\n",
26+
"* [Code](#Code)\n",
27+
"* [Unit Test](#Unit-Test)\n",
28+
"* [Solution Notebook](#Solution-Notebook)"
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"metadata": {},
34+
"source": [
35+
"## Constraints\n",
36+
"\n",
37+
"* Do we expect the methods to be insert, extract_min, and decrease_key?\n",
38+
" * Yes\n",
39+
"* Can we assume there aren't any duplicate keys?\n",
40+
" * Yes\n",
41+
"* Do we need to validate inputs?\n",
42+
" * No\n",
43+
"* Can we assume this fits memory?\n",
44+
" * Yes"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"## Test Cases\n",
52+
"\n",
53+
"### insert\n",
54+
"\n",
55+
"* `insert` general case -> inserted node\n",
56+
"\n",
57+
"### extract_min\n",
58+
"\n",
59+
"* `extract_min` from an empty list -> None\n",
60+
"* `extract_min` general case -> min node\n",
61+
"\n",
62+
"### decrease_key\n",
63+
"\n",
64+
"* `decrease_key` an invalid key -> None\n",
65+
"* `decrease_key` general case -> updated node"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"## Algorithm\n",
73+
"\n",
74+
"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."
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"metadata": {},
80+
"source": [
81+
"## Code"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"metadata": {
88+
"collapsed": true
89+
},
90+
"outputs": [],
91+
"source": [
92+
"class PriorityQueueNode(object):\n",
93+
"\n",
94+
" def __init__(self, obj, key):\n",
95+
" self.obj = obj\n",
96+
" self.key = key\n",
97+
"\n",
98+
" def __repr__(self):\n",
99+
" return str(self.obj) + ': ' + str(self.key)\n",
100+
"\n",
101+
"\n",
102+
"class PriorityQueue(object):\n",
103+
"\n",
104+
" def __init__(self):\n",
105+
" self.array = []\n",
106+
"\n",
107+
" def __len__(self):\n",
108+
" return len(self.array)\n",
109+
"\n",
110+
" def insert(self, node):\n",
111+
" # TODO: Implement me\n",
112+
" pass\n",
113+
"\n",
114+
" def extract_min(self):\n",
115+
" # TODO: Implement me\n",
116+
" pass\n",
117+
"\n",
118+
" def decrease_key(self, obj, new_key):\n",
119+
" # TODO: Implement me\n",
120+
" pass"
121+
]
122+
},
123+
{
124+
"cell_type": "markdown",
125+
"metadata": {},
126+
"source": [
127+
"## Unit Test"
128+
]
129+
},
130+
{
131+
"cell_type": "markdown",
132+
"metadata": {},
133+
"source": [
134+
"**The following unit test is expected to fail until you solve the challenge.**"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": null,
140+
"metadata": {
141+
"collapsed": false
142+
},
143+
"outputs": [],
144+
"source": [
145+
"# %load test_priority_queue.py\n",
146+
"from nose.tools import assert_equal\n",
147+
"\n",
148+
"\n",
149+
"class TestPriorityQueue(object):\n",
150+
"\n",
151+
" def test_priority_queue(self):\n",
152+
" priority_queue = PriorityQueue()\n",
153+
" assert_equal(priority_queue.extract_min(), None)\n",
154+
" priority_queue.insert(PriorityQueueNode('a', 20))\n",
155+
" priority_queue.insert(PriorityQueueNode('b', 5))\n",
156+
" priority_queue.insert(PriorityQueueNode('c', 15))\n",
157+
" priority_queue.insert(PriorityQueueNode('d', 22))\n",
158+
" priority_queue.insert(PriorityQueueNode('e', 40))\n",
159+
" priority_queue.insert(PriorityQueueNode('f', 3))\n",
160+
" priority_queue.decrease_key('f', 2)\n",
161+
" priority_queue.decrease_key('a', 19)\n",
162+
" mins = []\n",
163+
" while priority_queue.array:\n",
164+
" mins.append(priority_queue.extract_min().key)\n",
165+
" assert_equal(mins, [2, 5, 15, 19, 22, 40])\n",
166+
" print('Success: test_min_heap')\n",
167+
"\n",
168+
"\n",
169+
"def main():\n",
170+
" test = TestPriorityQueue()\n",
171+
" test.test_priority_queue()\n",
172+
"\n",
173+
"\n",
174+
"if __name__ == '__main__':\n",
175+
" main()"
176+
]
177+
},
178+
{
179+
"cell_type": "markdown",
180+
"metadata": {},
181+
"source": [
182+
"## Solution Notebook\n",
183+
"\n",
184+
"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."
185+
]
186+
}
187+
],
188+
"metadata": {
189+
"kernelspec": {
190+
"display_name": "Python 3",
191+
"language": "python",
192+
"name": "python3"
193+
},
194+
"language_info": {
195+
"codemirror_mode": {
196+
"name": "ipython",
197+
"version": 3
198+
},
199+
"file_extension": ".py",
200+
"mimetype": "text/x-python",
201+
"name": "python",
202+
"nbconvert_exporter": "python",
203+
"pygments_lexer": "ipython3",
204+
"version": "3.5.0"
205+
}
206+
},
207+
"nbformat": 4,
208+
"nbformat_minor": 0
209+
}

0 commit comments

Comments
 (0)