interactive-coding-challenges/graphs_trees/trie/trie_challenge.ipynb

280 lines
7.3 KiB
Python
Raw Normal View History

2017-03-30 17:38:16 +08:00
{
"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 trie with find, insert, remove, and list_words 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 we are working with strings?\n",
" * Yes\n",
"* Are the strings in ASCII?\n",
" * Yes\n",
"* Should `find` only match exact words with a terminating character?\n",
" * Yes\n",
"* Should `list_words` only return words with a terminating character?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"<pre>\n",
"\n",
" root\n",
" / | \\\n",
" h a* m\n",
" / \\ \\ \\\n",
" a e* t* e*\n",
" / \\ / \\\n",
" s* t* n* t*\n",
" /\n",
" s*\n",
"\n",
"find\n",
"\n",
"* Find on an empty trie\n",
"* Find non-matching\n",
"* Find matching\n",
"\n",
"insert\n",
"\n",
"* Insert on empty trie\n",
"* Insert to make a leaf terminator char\n",
"* Insert to extend an existing terminator char\n",
"\n",
"remove\n",
"\n",
"* Remove me\n",
"* Remove mens\n",
"* Remove a\n",
"* Remove has\n",
"\n",
"list_words\n",
"\n",
"* List empty\n",
"* List general case\n",
"</pre>\n",
"\n"
]
},
{
"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/trie/trie_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": [
"from collections import OrderedDict\n",
"\n",
"\n",
"class Node(object):\n",
"\n",
" def __init__(self, data, parent=None, terminates=False):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
"\n",
"class Trie(object):\n",
"\n",
" def __init__(self):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def find(self, word):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def insert(self, word):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def remove(self, word):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def list_words(self):\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_trie.py\n",
"from nose.tools import assert_true\n",
"\n",
"\n",
"class TestTrie(object):\n",
"\n",
" def test_trie(self):\n",
" print('Test: Remove from empty trie')\n",
" trie = Trie()\n",
" assert_true(trie.remove('foo') is None)\n",
"\n",
" print('Test: Insert')\n",
" words = ['a', 'at', 'has', 'hat', 'he',\n",
" 'me', 'men', 'mens', 'met']\n",
" for word in words:\n",
" trie.insert(word)\n",
" for word in trie.list_words():\n",
" assert_true(trie.find(word) is not None)\n",
"\n",
" # Remove me\n",
" # Remove mens\n",
" # Remove a\n",
" \n",
" print('Test: Remove me')\n",
" trie.remove('me')\n",
" words_removed = ['me']\n",
" words = ['a', 'at', 'has', 'hat', 'he',\n",
" 'men', 'mens', 'met']\n",
" for word in words:\n",
" assert_true(trie.find(word) is not None)\n",
" for word in words_removed:\n",
" assert_true(trie.find(word) is None)\n",
"\n",
" print('Test: Remove mens')\n",
" trie.remove('mens')\n",
" words_removed = ['me', 'mens']\n",
" words = ['a', 'at', 'has', 'hat', 'he',\n",
" 'men', 'met']\n",
" for word in words:\n",
" assert_true(trie.find(word) is not None)\n",
" for word in words_removed:\n",
" assert_true(trie.find(word) is None)\n",
"\n",
" print('Test: Remove a')\n",
" trie.remove('a')\n",
" words_removed = ['a', 'me', 'mens']\n",
" words = ['at', 'has', 'hat', 'he',\n",
" 'men', 'met']\n",
" for word in words:\n",
" assert_true(trie.find(word) is not None)\n",
" for word in words_removed:\n",
" assert_true(trie.find(word) is None)\n",
"\n",
" print('Test: Remove has')\n",
" trie.remove('has')\n",
" words_removed = ['a', 'has', 'me', 'mens']\n",
" words = ['at', 'hat', 'he',\n",
" 'men', 'met']\n",
" for word in words:\n",
" assert_true(trie.find(word) is not None)\n",
" for word in words_removed:\n",
" assert_true(trie.find(word) is None)\n",
"\n",
" print('Success: test_trie')\n",
"\n",
"\n",
"def main():\n",
" test = TestTrie()\n",
" test.test_trie()\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/trie/trie_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
}