Reworked notebook: Added more detail to clarifying questions and test cases. Reworked unit test.

This commit is contained in:
Donne Martin
2015-06-24 18:25:48 -04:00
parent 5ceedbd1e3
commit 46b757a372

View File

@@ -20,7 +20,8 @@
"* [Algorithm 2: Hash Map Lookup](#Algorithm-2:-Hash-Map-Lookup)\n", "* [Algorithm 2: Hash Map Lookup](#Algorithm-2:-Hash-Map-Lookup)\n",
"* [Code: Hash Map Lookup](#Code:-Hash-Map-Lookup)\n", "* [Code: Hash Map Lookup](#Code:-Hash-Map-Lookup)\n",
"* [Algorithm 3: In-Place](#Algorithm-3:-In-Place)\n", "* [Algorithm 3: In-Place](#Algorithm-3:-In-Place)\n",
"* [Code: In-Place](#Code:-In-Place)" "* [Code: In-Place](#Code:-In-Place)\n",
"* [Unit Test](#Unit-Test)"
] ]
}, },
{ {
@@ -28,8 +29,12 @@
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Clarifying Questions\n", "## Clarifying Questions\n",
"* Is the string in ASCII (extended?) or Unicode? \n", "\n",
" * ASCII extended, which is 256 characters\n", "*Problem statements are sometimes intentionally ambiguous. Asking clarifying questions, identifying constraints, and stating assumptions help to ensure you code the intended solution.*\n",
"\n",
"* Can I assume the string is ASCII?\n",
" * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n",
"* Can you use additional data structures? \n", "* Can you use additional data structures? \n",
" * Yes" " * Yes"
] ]
@@ -40,32 +45,13 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"*Identifying and running through general and edge cases are important. You generally will not be asked to write a unit test like what is shown below.*\n",
"\n",
"* '' -> True\n", "* '' -> True\n",
"* 'foo' -> False\n", "* 'foo' -> False\n",
"* 'bar' -> True" "* 'bar' -> True"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
" def test_unique_chars(self, func):\n",
" assert_equal(func(''), True)\n",
" assert_equal(func('foo'), False)\n",
" assert_equal(func('bar'), True)\n",
"\n",
"def run_tests(func):\n",
" test = Test()\n",
" test.test_unique_chars(func)"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -93,16 +79,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars(string):\n", "def unique_chars(string):\n",
" return len(set(string)) == len(string)\n", " return len(set(string)) == len(string)"
"\n",
"run_tests(unique_chars)"
] ]
}, },
{ {
@@ -138,7 +122,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@@ -151,9 +135,7 @@
" return False\n", " return False\n",
" else:\n", " else:\n",
" chars_set.add(char)\n", " chars_set.add(char)\n",
" return True\n", " return True"
"\n",
"run_tests(unique_chars_hash)"
] ]
}, },
{ {
@@ -184,7 +166,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 3,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@@ -194,9 +176,48 @@
" for char in string:\n", " for char in string:\n",
" if string.count(char) > 1:\n", " if string.count(char) > 1:\n",
" return False\n", " return False\n",
" return True\n", " return True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_unique_chars\n",
"Success: test_unique_chars\n",
"Success: test_unique_chars\n"
]
}
],
"source": [
"from nose.tools import assert_equal\n",
"\n", "\n",
"run_tests(unique_chars_inplace)" "class Test(object):\n",
" def test_unique_chars(self, func):\n",
" assert_equal(func(''), True)\n",
" assert_equal(func('foo'), False)\n",
" assert_equal(func('bar'), True)\n",
" print('Success: test_unique_chars')\n",
"\n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_unique_chars(unique_chars)\n",
" test.test_unique_chars(unique_chars_hash)\n",
" test.test_unique_chars(unique_chars_inplace)"
] ]
} }
], ],