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:23:56 -04:00
parent 6cea5d003a
commit 04a68f5ef5

View File

@ -16,7 +16,8 @@
"* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)"
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
@ -25,8 +26,11 @@
"source": [
"## Clarifying Questions\n",
"\n",
"* Is the string ASCII (extended)? Or Unicode?\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",
" * Yes\n",
"* Is this case sensitive?\n",
@ -39,6 +43,8 @@
"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",
"* Any strings that differ in size results in False\n",
"* NULL, 'foo' -> False (any NULL results in False)\n",
"* ' ', 'foo' -> False\n",
@ -46,29 +52,6 @@
"* 'foobarbaz', 'barbazfoo' -> True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
" def test_is_rotation(self, func):\n",
" assert_equal(func('o', 'oo'), False)\n",
" assert_equal(func(None, 'foo'), False)\n",
" assert_equal(func('', 'foo'), False)\n",
" assert_equal(func('', ''), True)\n",
" assert_equal(func('foobarbaz', 'barbazfoo'), True)\n",
"\n",
"def run_tests(func):\n",
" test = Test()\n",
" test.test_is_rotation(func)"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -97,7 +80,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"collapsed": false
},
@ -112,9 +95,46 @@
" if len(s1) != len(s2):\n",
" return False\n",
" s3 = s1 + s1\n",
" return is_substring(s2, s3)\n",
" return is_substring(s2, s3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_is_rotation\n"
]
}
],
"source": [
"from nose.tools import assert_equal\n",
"\n",
"run_tests(is_rotation)"
"class Test(object):\n",
" def test_is_rotation(self, func):\n",
" assert_equal(func('o', 'oo'), False)\n",
" assert_equal(func(None, 'foo'), False)\n",
" assert_equal(func('', 'foo'), False)\n",
" assert_equal(func('', ''), True)\n",
" assert_equal(func('foobarbaz', 'barbazfoo'), True)\n",
" print('Success: test_is_rotation')\n",
"\n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_is_rotation(is_rotation)"
]
}
],