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 code and unit test.
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
"* [Algorithm](#Algorithm)\n",
|
"* [Algorithm](#Algorithm)\n",
|
||||||
"* [Code](#Code)\n",
|
"* [Code](#Code)\n",
|
||||||
"* [Pythonic-Code](#Pythonic-Code)\n",
|
"* [Pythonic-Code](#Pythonic-Code)\n",
|
||||||
|
"* [Unit Test](#Unit-Test)\n",
|
||||||
"* [C Algorithm](C-Algorithm)\n",
|
"* [C Algorithm](C-Algorithm)\n",
|
||||||
"* [C Code]()"
|
"* [C Code]()"
|
||||||
]
|
]
|
||||||
@@ -28,6 +29,11 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Clarifying Questions\n",
|
"## Clarifying Questions\n",
|
||||||
"\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 I use the slice operator or the reversed function?\n",
|
"* Can I use the slice operator or the reversed function?\n",
|
||||||
" * No\n",
|
" * No\n",
|
||||||
"* Since Python string are immutable, can I use a list instead?\n",
|
"* Since Python string are immutable, can I use a list instead?\n",
|
||||||
@@ -40,32 +46,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",
|
||||||
"* NULL input\n",
|
"* NULL input\n",
|
||||||
"* '' -> ''\n",
|
"* '' -> ''\n",
|
||||||
"* 'foo bar' -> 'rab oof'"
|
"* 'foo bar' -> 'rab oof'"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"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_reversed(self, func):\n",
|
|
||||||
" assert_equal(func(None), None)\n",
|
|
||||||
" assert_equal(func(''), '')\n",
|
|
||||||
" assert_equal(func('foo bar'), 'rab oof')\n",
|
|
||||||
"\n",
|
|
||||||
"def run_tests(func):\n",
|
|
||||||
" test = Test()\n",
|
|
||||||
" test.test_reversed(func)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
@@ -92,7 +79,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
@@ -104,11 +91,9 @@
|
|||||||
" string_list = list(string)\n",
|
" string_list = list(string)\n",
|
||||||
" string_length = len(string_list)\n",
|
" string_length = len(string_list)\n",
|
||||||
" for i in xrange(string_length/2):\n",
|
" for i in xrange(string_length/2):\n",
|
||||||
" string_list[i], string_list[string_length - 1 - i] = \\\n",
|
" string_list[i], string_list[string_length-1-i] = \\\n",
|
||||||
" string_list[string_length - 1 - i], string_list[i]\n",
|
" string_list[string_length-1-i], string_list[i]\n",
|
||||||
" return ''.join(string_list)\n",
|
" return ''.join(string_list)"
|
||||||
"\n",
|
|
||||||
"run_tests(reverse_string)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -122,7 +107,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
@@ -136,10 +121,48 @@
|
|||||||
"def reverse_string_alt2(string):\n",
|
"def reverse_string_alt2(string):\n",
|
||||||
" if string is None:\n",
|
" if string is None:\n",
|
||||||
" return None \n",
|
" return None \n",
|
||||||
" return ''.join(reversed(string))\n",
|
" return ''.join(reversed(string))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Unit Test"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Success: test_reversed\n",
|
||||||
|
"Success: test_reversed\n",
|
||||||
|
"Success: test_reversed\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from nose.tools import assert_equal\n",
|
||||||
"\n",
|
"\n",
|
||||||
"run_tests(reverse_string_alt)\n",
|
"class Test(object):\n",
|
||||||
"run_tests(reverse_string_alt2)"
|
" def test_reversed(self, func):\n",
|
||||||
|
" assert_equal(func(None), None)\n",
|
||||||
|
" assert_equal(func(''), '')\n",
|
||||||
|
" assert_equal(func('foo bar'), 'rab oof')\n",
|
||||||
|
" print('Success: test_reversed')\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" test = Test()\n",
|
||||||
|
" test.test_reversed(reverse_string)\n",
|
||||||
|
" test.test_reversed(reverse_string_alt)\n",
|
||||||
|
" test.test_reversed(reverse_string_alt2)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user