mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-08 18:38:03 +00:00
Reworked notebook: Added more detail to clarifying questions and test cases. Reworked unit test.
This commit is contained in:
@@ -20,7 +20,8 @@
|
||||
"* [Algorithm 2: Hash Map Lookup](#Algorithm-2:-Hash-Map-Lookup)\n",
|
||||
"* [Code: Hash Map Lookup](#Code:-Hash-Map-Lookup)\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": {},
|
||||
"source": [
|
||||
"## Clarifying Questions\n",
|
||||
"* Is the string in ASCII (extended?) or Unicode? \n",
|
||||
" * ASCII extended, which is 256 characters\n",
|
||||
"\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",
|
||||
" * Yes"
|
||||
]
|
||||
@@ -40,32 +45,13 @@
|
||||
"source": [
|
||||
"## Test Cases\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",
|
||||
"* 'foo' -> False\n",
|
||||
"* '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",
|
||||
"metadata": {},
|
||||
@@ -93,16 +79,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def unique_chars(string):\n",
|
||||
" return len(set(string)) == len(string)\n",
|
||||
"\n",
|
||||
"run_tests(unique_chars)"
|
||||
" return len(set(string)) == len(string)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -138,7 +122,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -151,9 +135,7 @@
|
||||
" return False\n",
|
||||
" else:\n",
|
||||
" chars_set.add(char)\n",
|
||||
" return True\n",
|
||||
"\n",
|
||||
"run_tests(unique_chars_hash)"
|
||||
" return True"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -184,7 +166,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -194,9 +176,48 @@
|
||||
" for char in string:\n",
|
||||
" if string.count(char) > 1:\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",
|
||||
"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)"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user