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

@ -40,6 +40,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"
]
},
@ -49,6 +53,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",
@ -115,6 +120,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",
@ -149,21 +155,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,

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",

View File

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