Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed solutions to challenge Arrays & Strings - Permutations: #234

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions arrays_strings/permutation/permutation_challenge.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -159,7 +160,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
Expand All @@ -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
}
71 changes: 62 additions & 9 deletions arrays_strings/permutation/permutation_solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 21,
"metadata": {
"collapsed": false
},
Expand All @@ -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)"
]
Expand Down Expand Up @@ -141,7 +141,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 24,
"metadata": {
"collapsed": false
},
Expand All @@ -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",
Expand All @@ -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": {},
Expand All @@ -175,7 +224,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 40,
"metadata": {
"collapsed": false
},
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -224,7 +276,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 41,
"metadata": {
"collapsed": false
},
Expand All @@ -233,6 +285,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_permutation\n",
"Success: test_permutation\n",
"Success: test_permutation\n"
]
Expand All @@ -245,7 +298,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
Expand All @@ -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
}
3 changes: 3 additions & 0 deletions arrays_strings/permutation/test_permutation_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down