From 895011b68528a886eedde8d7f30ec58ba586d382 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sun, 12 Jun 2016 23:19:09 -0400 Subject: [PATCH] Polish unique chars challenge and solution Add None input test case. Update constraints. --- .../unique_chars/test_unique_chars.py | 1 + .../unique_chars/unique_chars_challenge.ipynb | 18 ++++++++------ .../unique_chars/unique_chars_solution.ipynb | 24 +++++++++++++------ 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/arrays_strings/unique_chars/test_unique_chars.py b/arrays_strings/unique_chars/test_unique_chars.py index 5dd6016..5219cf5 100644 --- a/arrays_strings/unique_chars/test_unique_chars.py +++ b/arrays_strings/unique_chars/test_unique_chars.py @@ -4,6 +4,7 @@ from nose.tools import assert_equal class TestUniqueChars(object): def test_unique_chars(self, func): + assert_equal(func(None), False) assert_equal(func(''), True) assert_equal(func('foo'), False) assert_equal(func('bar'), True) diff --git a/arrays_strings/unique_chars/unique_chars_challenge.ipynb b/arrays_strings/unique_chars/unique_chars_challenge.ipynb index 91ea17c..1f391ff 100644 --- a/arrays_strings/unique_chars/unique_chars_challenge.ipynb +++ b/arrays_strings/unique_chars/unique_chars_challenge.ipynb @@ -34,12 +34,14 @@ "source": [ "## Constraints\n", "\n", - "* Can you assume the string is ASCII?\n", + "* Can we assume the string is ASCII?\n", " * Yes\n", " * Note: Unicode strings could require special handling depending on your language\n", "* Can we assume this is case sensitive?\n", " * Yes\n", - "* Can you use additional data structures?\n", + "* Can we use additional data structures?\n", + " * Yes\n", + "* Can we assume this fits in memory?\n", " * Yes" ] }, @@ -49,6 +51,7 @@ "source": [ "## Test Cases\n", "\n", + "* None -> False\n", "* '' -> True\n", "* 'foo' -> False\n", "* 'bar' -> True" @@ -112,6 +115,7 @@ "class TestUniqueChars(object):\n", "\n", " def test_unique_chars(self, func):\n", + " assert_equal(func(None), False)\n", " assert_equal(func(''), True)\n", " assert_equal(func('foo'), False)\n", " assert_equal(func('bar'), True)\n", @@ -146,21 +150,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.5.0" } }, "nbformat": 4, diff --git a/arrays_strings/unique_chars/unique_chars_solution.ipynb b/arrays_strings/unique_chars/unique_chars_solution.ipynb index 4c0c787..38def41 100644 --- a/arrays_strings/unique_chars/unique_chars_solution.ipynb +++ b/arrays_strings/unique_chars/unique_chars_solution.ipynb @@ -37,12 +37,14 @@ "source": [ "## Constraints\n", "\n", - "* Can you assume the string is ASCII?\n", + "* Can we assume the string is ASCII?\n", " * Yes\n", " * Note: Unicode strings could require special handling depending on your language\n", "* Can we assume this is case sensitive?\n", " * Yes\n", - "* Can you use additional data structures? \n", + "* Can we use additional data structures?\n", + " * Yes\n", + "* Can we assume this fits in memory?\n", " * Yes" ] }, @@ -52,6 +54,7 @@ "source": [ "## Test Cases\n", "\n", + "* None -> False\n", "* '' -> True\n", "* 'foo' -> False\n", "* 'bar' -> True" @@ -91,6 +94,8 @@ "outputs": [], "source": [ "def unique_chars(string):\n", + " if string is None:\n", + " return False\n", " return len(set(string)) == len(string)" ] }, @@ -134,6 +139,8 @@ "outputs": [], "source": [ "def unique_chars_hash(string):\n", + " if string is None:\n", + " return False\n", " chars_set = set()\n", " for char in string:\n", " if char in chars_set:\n", @@ -178,6 +185,8 @@ "outputs": [], "source": [ "def unique_chars_inplace(string):\n", + " if string is None:\n", + " return False\n", " for char in string:\n", " if string.count(char) > 1:\n", " return False\n", @@ -214,6 +223,7 @@ "class TestUniqueChars(object):\n", "\n", " def test_unique_chars(self, func):\n", + " assert_equal(func(None), False)\n", " assert_equal(func(''), True)\n", " assert_equal(func('foo'), False)\n", " assert_equal(func('bar'), True)\n", @@ -260,21 +270,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.5.0" } }, "nbformat": 4,