From 5ceedbd1e36f1caa2bced1b4103e13252ef618c9 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Wed, 24 Jun 2015 18:24:40 -0400 Subject: [PATCH] Reworked notebook: Added more detail to clarifying questions and test cases. Reworked code and unit test. --- arrays-strings/reverse_string.ipynb | 85 ++++++++++++++++++----------- 1 file changed, 54 insertions(+), 31 deletions(-) diff --git a/arrays-strings/reverse_string.ipynb b/arrays-strings/reverse_string.ipynb index 9cebfe7..d7d0515 100644 --- a/arrays-strings/reverse_string.ipynb +++ b/arrays-strings/reverse_string.ipynb @@ -18,6 +18,7 @@ "* [Algorithm](#Algorithm)\n", "* [Code](#Code)\n", "* [Pythonic-Code](#Pythonic-Code)\n", + "* [Unit Test](#Unit-Test)\n", "* [C Algorithm](C-Algorithm)\n", "* [C Code]()" ] @@ -28,6 +29,11 @@ "source": [ "## Clarifying Questions\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", " * No\n", "* Since Python string are immutable, can I use a list instead?\n", @@ -40,32 +46,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", "* NULL input\n", "* '' -> ''\n", "* '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", "metadata": {}, @@ -92,7 +79,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -104,11 +91,9 @@ " string_list = list(string)\n", " string_length = len(string_list)\n", " for i in xrange(string_length/2):\n", - " string_list[i], string_list[string_length - 1 - i] = \\\n", - " string_list[string_length - 1 - i], string_list[i]\n", - " return ''.join(string_list)\n", - "\n", - "run_tests(reverse_string)" + " string_list[i], string_list[string_length-1-i] = \\\n", + " string_list[string_length-1-i], string_list[i]\n", + " return ''.join(string_list)" ] }, { @@ -122,7 +107,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -136,10 +121,48 @@ "def reverse_string_alt2(string):\n", " if string is 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", - "run_tests(reverse_string_alt)\n", - "run_tests(reverse_string_alt2)" + "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", + " 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)" ] }, {