Polish permutation challenge and solution

This commit is contained in:
Donne Martin
2016-06-05 07:18:14 -04:00
parent 730e8a0d67
commit 1656fd50e3
3 changed files with 22 additions and 5 deletions

View File

@@ -41,6 +41,10 @@
"* Is whitespace important?\n",
" * Yes\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"
]
},
@@ -50,6 +54,7 @@
"source": [
"## Test Cases\n",
"\n",
"* One or more None inputs -> False\n",
"* One or more empty strings -> False\n",
"* 'Nib', 'bin' -> False\n",
"* 'act', 'cat' -> True\n",
@@ -91,6 +96,8 @@
"outputs": [],
"source": [
"def permutations(str1, str2):\n",
" if str1 is None or str2 is None:\n",
" return False\n",
" return sorted(str1) == sorted(str2)"
]
},
@@ -141,6 +148,8 @@
"\n",
"\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",
" return False\n",
" unique_counts1 = defaultdict(int)\n",
@@ -182,6 +191,7 @@
"class TestPermutation(object):\n",
"\n",
" def test_permutation(self, func):\n",
" assert_equal(func(None, 'foo'), False)\n",
" assert_equal(func('', 'foo'), False)\n",
" assert_equal(func('Nib', 'bin'), False)\n",
" assert_equal(func('act', 'cat'), True)\n",