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

This commit is contained in:
Donne Martin 2015-06-24 18:22:25 -04:00
parent 786f33eb72
commit 88935b61f0

View File

@ -18,7 +18,8 @@
"* [Algorithm: Compare Sorted Strings](#Algorithm:-Compare-Sorted-Strings)\n", "* [Algorithm: Compare Sorted Strings](#Algorithm:-Compare-Sorted-Strings)\n",
"* [Code: Compare Sorted Strings](#Code:-Compare-Sorted-Strings)\n", "* [Code: Compare Sorted Strings](#Code:-Compare-Sorted-Strings)\n",
"* [Algorithm: Hashmap Lookup](#Algorithm:-Hash-Map-Lookup)\n", "* [Algorithm: Hashmap Lookup](#Algorithm:-Hash-Map-Lookup)\n",
"* [Code: Hashmap Lookup](#Code:-Hash-Map-Lookup)" "* [Code: Hashmap Lookup](#Code:-Hash-Map-Lookup)\n",
"* [Unit Test](#Unit-Test)"
] ]
}, },
{ {
@ -27,8 +28,11 @@
"source": [ "source": [
"## Clarifying Questions\n", "## Clarifying Questions\n",
"\n", "\n",
"* Is the string ASCII (extended)? Or Unicode?\n", "*Problem statements are sometimes intentionally ambiguous. Asking clarifying questions, identifying constraints, and stating assumptions help to ensure you code the intended solution.*\n",
" * ASCII extended, which is 256 characters\n", "\n",
"* Can I assume the string is ASCII?\n",
" * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n",
"* Is whitespace important?\n", "* Is whitespace important?\n",
" * Yes\n", " * Yes\n",
"* Is this case sensitive? 'Nib', 'bin' is not a match?\n", "* Is this case sensitive? 'Nib', 'bin' is not a match?\n",
@ -41,34 +45,14 @@
"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",
"* One or more empty strings -> False\n", "* One or more empty strings -> False\n",
"* 'Nib', 'bin' -> False\n", "* 'Nib', 'bin' -> False\n",
"* 'act', 'cat' -> True\n", "* 'act', 'cat' -> True\n",
"* 'a ct', 'ca t' -> True" "* 'a ct', 'ca t' -> 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_permutation(self, func):\n",
" assert_equal(func('', 'foo'), False)\n",
" assert_equal(func('Nib', 'bin'), False)\n",
" assert_equal(func('act', 'cat'), True)\n",
" assert_equal(func('a ct', 'ca t'), True)\n",
"\n",
"def run_tests(func):\n",
" test = Test()\n",
" test.test_permutation(func)"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -97,16 +81,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def permutations(str1, str2):\n", "def permutations(str1, str2):\n",
" return sorted(str1) == sorted(str2)\n", " return sorted(str1) == sorted(str2)"
"\n",
"run_tests(permutations)"
] ]
}, },
{ {
@ -128,7 +110,7 @@
" * Return False\n", " * Return False\n",
"\n", "\n",
"Notes:\n", "Notes:\n",
"* Since the characters are in ASCII, we could potentially use an array of size 128 (or 256 for extended ASCII)\n", "* Since the characters are in ASCII, we could potentially use an array of size 128 (or 256 for extended ASCII), where each array index is equivalent to an ASCII value\n",
"* Instead of using two hash maps, you could use one hash map and increment character values based on the first string and decrement based on the second string\n", "* Instead of using two hash maps, you could use one hash map and increment character values based on the first string and decrement based on the second string\n",
"* You can short circuit if the lengths of each string are not equal, len() in Python is generally O(1)\n", "* You can short circuit if the lengths of each string are not equal, len() in Python is generally O(1)\n",
"\n", "\n",
@ -146,7 +128,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -165,9 +147,47 @@
" return False\n", " return False\n",
" unique_counts1 = unique_counts(str1)\n", " unique_counts1 = unique_counts(str1)\n",
" unique_counts2 = unique_counts(str2)\n", " unique_counts2 = unique_counts(str2)\n",
" return unique_counts1 == unique_counts2\n", " return unique_counts1 == unique_counts2"
]
},
{
"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_permutation\n",
"Success: test_permutation\n"
]
}
],
"source": [
"from nose.tools import assert_equal\n",
"\n", "\n",
"run_tests(permutations_alt)" "class Test(object):\n",
" def test_permutation(self, func):\n",
" assert_equal(func('', 'foo'), False)\n",
" assert_equal(func('Nib', 'bin'), False)\n",
" assert_equal(func('act', 'cat'), True)\n",
" assert_equal(func('a ct', 'ca t'), True)\n",
" print('Success: test_permutation')\n",
"\n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_permutation(permutations)\n",
" test.test_permutation(permutations_alt)"
] ]
} }
], ],