From 88935b61f0907a379e4d0396ca8adf31d68ce67d Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Wed, 24 Jun 2015 18:22:25 -0400 Subject: [PATCH] Reworked notebook: Added more detail to clarifying questions and test cases. Reworked algorithm discussion and unit test. --- arrays-strings/permutation.ipynb | 86 ++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 33 deletions(-) diff --git a/arrays-strings/permutation.ipynb b/arrays-strings/permutation.ipynb index 4a4afb1..266bb2f 100644 --- a/arrays-strings/permutation.ipynb +++ b/arrays-strings/permutation.ipynb @@ -18,7 +18,8 @@ "* [Algorithm: Compare Sorted Strings](#Algorithm:-Compare-Sorted-Strings)\n", "* [Code: Compare Sorted Strings](#Code:-Compare-Sorted-Strings)\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": [ "## 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", "* Is whitespace important?\n", " * Yes\n", "* Is this case sensitive? 'Nib', 'bin' is not a match?\n", @@ -41,34 +45,14 @@ "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", "* One or more empty strings -> False\n", "* 'Nib', 'bin' -> False\n", "* 'act', 'cat' -> True\n", "* '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", "metadata": {}, @@ -97,16 +81,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def permutations(str1, str2):\n", - " return sorted(str1) == sorted(str2)\n", - "\n", - "run_tests(permutations)" + " return sorted(str1) == sorted(str2)" ] }, { @@ -128,7 +110,7 @@ " * Return False\n", "\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", "* You can short circuit if the lengths of each string are not equal, len() in Python is generally O(1)\n", "\n", @@ -146,7 +128,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -165,9 +147,47 @@ " return False\n", " unique_counts1 = unique_counts(str1)\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", - "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)" ] } ],