changed solutions to challenge Arrays & Strings:

added a test for checking two empty strings, the proposed solutions did not pass this test although the specification requires it to return False in this case

changed the proposed solutions to pass this additional test

added a new solution
This commit is contained in:
derichard 2018-04-19 17:28:55 +02:00
parent d898520439
commit c6d8f406fd
3 changed files with 69 additions and 12 deletions

View File

@ -124,6 +124,7 @@
" def test_permutation(self, func):\n",
" assert_equal(func(None, 'foo'), False)\n",
" assert_equal(func('', 'foo'), False)\n",
" assert_equal(func('', ''), False)\n",
" assert_equal(func('Nib', 'bin'), False)\n",
" assert_equal(func('act', 'cat'), True)\n",
" assert_equal(func('a ct', 'ca t'), True)\n",
@ -159,7 +160,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -173,9 +174,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 2
}

View File

@ -90,7 +90,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 21,
"metadata": {
"collapsed": false
},
@ -99,7 +99,7 @@
"class Permutations(object):\n",
"\n",
" def is_permutation(self, str1, str2):\n",
" if str1 is None or str2 is None:\n",
" if not str1 or not str2:\n",
" return False\n",
" return sorted(str1) == sorted(str2)"
]
@ -141,7 +141,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 24,
"metadata": {
"collapsed": false
},
@ -153,7 +153,7 @@
"class PermutationsAlt(object):\n",
"\n",
" def is_permutation(self, str1, str2):\n",
" if str1 is None or str2 is None:\n",
" if not str1 or not str2:\n",
" return False\n",
" if len(str1) != len(str2):\n",
" return False\n",
@ -166,6 +166,55 @@
" return unique_counts1 == unique_counts2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm: Array Removal\n",
" \n",
"Since a permuation is a symmetric relation (A is a permutation of B iff B is a permutation of A) one only needs to check if one can rebuild the first string using all the characters of the second.\n",
"\n",
"Steps:\n",
"* Check if the strings are not None or empty\n",
"* Turn the second string into a list of characters\n",
"* For each character in the first string:\n",
" * If the character does not appear in the list, return False\n",
" * Else: remove the character from the list\n",
"* Return True if the list is empty (we used up all characters), else return False\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: O(n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code: Array Lookup"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"class PermutationsArray(object):\n",
"\n",
" def is_permutation(self, str1, str2):\n",
" if not str1 or not str2:\n",
" return False\n",
" \n",
" chars = list(str2) \n",
" for char in str1:\n",
" if char not in chars:\n",
" return False\n",
" else:\n",
" chars.remove(char)\n",
" return len(chars) == 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -175,7 +224,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 40,
"metadata": {
"collapsed": false
},
@ -198,6 +247,7 @@
" def test_permutation(self, func):\n",
" assert_equal(func(None, 'foo'), False)\n",
" assert_equal(func('', 'foo'), False)\n",
" assert_equal(func('', ''), False)\n",
" assert_equal(func('Nib', 'bin'), False)\n",
" assert_equal(func('act', 'cat'), True)\n",
" assert_equal(func('a ct', 'ca t'), True)\n",
@ -212,6 +262,8 @@
" try:\n",
" permutations_alt = PermutationsAlt()\n",
" test.test_permutation(permutations_alt.is_permutation)\n",
" permutations_array = PermutationsArray()\n",
" test.test_permutation(permutations_array.is_permutation)\n",
" except NameError:\n",
" # Alternate solutions are only defined\n",
" # in the solutions file\n",
@ -224,7 +276,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 41,
"metadata": {
"collapsed": false
},
@ -233,6 +285,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_permutation\n",
"Success: test_permutation\n",
"Success: test_permutation\n"
]
@ -245,7 +298,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@ -259,9 +312,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 2
}

View File

@ -6,6 +6,7 @@ class TestPermutation(object):
def test_permutation(self, func):
assert_equal(func(None, 'foo'), False)
assert_equal(func('', 'foo'), False)
assert_equal(func('', ''), False)
assert_equal(func('Nib', 'bin'), False)
assert_equal(func('act', 'cat'), True)
assert_equal(func('a ct', 'ca t'), True)
@ -20,6 +21,8 @@ def main():
try:
permutations_alt = PermutationsAlt()
test.test_permutation(permutations_alt.is_permutation)
permutations_array = PermutationsArray()
test.test_permutation(permutations_array.is_permutation)
except NameError:
# Alternate solutions are only defined
# in the solutions file