Merge pull request #54 from donnemartin/develop

Polish permutation challenge and solution
This commit is contained in:
Donne Martin 2016-06-05 07:18:54 -04:00
commit 1bbe9c68d5
3 changed files with 22 additions and 5 deletions

View File

@ -40,6 +40,10 @@
"* 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",
" * Yes\n",
"* Can we use additional data structures?\n",
" * Yes\n",
"* Can we assume this fits in memory?\n",
" * Yes" " * Yes"
] ]
}, },
@ -49,6 +53,7 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* One or more None inputs -> False\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",
@ -115,6 +120,7 @@
"class TestPermutation(object):\n", "class TestPermutation(object):\n",
"\n", "\n",
" def test_permutation(self, func):\n", " def test_permutation(self, func):\n",
" assert_equal(func(None, 'foo'), False)\n",
" assert_equal(func('', 'foo'), False)\n", " assert_equal(func('', 'foo'), False)\n",
" assert_equal(func('Nib', 'bin'), False)\n", " assert_equal(func('Nib', 'bin'), False)\n",
" assert_equal(func('act', 'cat'), True)\n", " assert_equal(func('act', 'cat'), True)\n",
@ -149,21 +155,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.10" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -41,6 +41,10 @@
"* 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",
" * Yes\n",
"* Can we use additional data structures?\n",
" * Yes\n",
"* Can we assume this fits in memory?\n",
" * Yes" " * Yes"
] ]
}, },
@ -50,6 +54,7 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* One or more None inputs -> False\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",
@ -91,6 +96,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def permutations(str1, str2):\n", "def permutations(str1, str2):\n",
" if str1 is None or str2 is None:\n",
" return False\n",
" return sorted(str1) == sorted(str2)" " return sorted(str1) == sorted(str2)"
] ]
}, },
@ -141,6 +148,8 @@
"\n", "\n",
"\n", "\n",
"def permutations_alt(str1, str2):\n", "def permutations_alt(str1, str2):\n",
" if str1 is None or str2 is None:\n",
" return False\n",
" if len(str1) != len(str2):\n", " if len(str1) != len(str2):\n",
" return False\n", " return False\n",
" unique_counts1 = defaultdict(int)\n", " unique_counts1 = defaultdict(int)\n",
@ -182,6 +191,7 @@
"class TestPermutation(object):\n", "class TestPermutation(object):\n",
"\n", "\n",
" def test_permutation(self, func):\n", " def test_permutation(self, func):\n",
" assert_equal(func(None, 'foo'), False)\n",
" assert_equal(func('', 'foo'), False)\n", " assert_equal(func('', 'foo'), False)\n",
" assert_equal(func('Nib', 'bin'), False)\n", " assert_equal(func('Nib', 'bin'), False)\n",
" assert_equal(func('act', 'cat'), True)\n", " assert_equal(func('act', 'cat'), True)\n",

View File

@ -4,6 +4,7 @@ from nose.tools import assert_equal
class TestPermutation(object): class TestPermutation(object):
def test_permutation(self, func): def test_permutation(self, func):
assert_equal(func(None, 'foo'), False)
assert_equal(func('', 'foo'), False) assert_equal(func('', 'foo'), False)
assert_equal(func('Nib', 'bin'), False) assert_equal(func('Nib', 'bin'), False)
assert_equal(func('act', 'cat'), True) assert_equal(func('act', 'cat'), True)