Move string permutation to a class

This commit is contained in:
Donne Martin
2016-08-13 06:39:19 -04:00
parent aa887db2e0
commit 22e3549d7d
3 changed files with 39 additions and 26 deletions

View File

@@ -84,9 +84,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def permutations(str1, str2):\n", "class Permutations(object):\n",
" # TODO: Implement me\n", "\n",
" pass" " def is_permutation(self, str1, str2):\n",
" # TODO: Implement me\n",
" pass"
] ]
}, },
{ {
@@ -130,9 +132,11 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestPermutation()\n", " test = TestPermutation()\n",
" test.test_permutation(permutations)\n", " permutations = Permutations()\n",
" test.test_permutation(permutations.is_permutation)\n",
" try:\n", " try:\n",
" test.test_permutation(permutations_alt)\n", " permutations_alt = PermutationsAlt()\n",
" test.test_permutation(permutations_alt.is_permutation)\n",
" except NameError:\n", " except NameError:\n",
" # Alternate solutions are only defined\n", " # Alternate solutions are only defined\n",
" # in the solutions file\n", " # in the solutions file\n",

View File

@@ -95,10 +95,12 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def permutations(str1, str2):\n", "class Permutations(object):\n",
" if str1 is None or str2 is None:\n", "\n",
" return False\n", " def is_permutation(self, str1, str2):\n",
" return sorted(str1) == sorted(str2)" " if str1 is None or str2 is None:\n",
" return False\n",
" return sorted(str1) == sorted(str2)"
] ]
}, },
{ {
@@ -147,18 +149,20 @@
"from collections import defaultdict\n", "from collections import defaultdict\n",
"\n", "\n",
"\n", "\n",
"def permutations_alt(str1, str2):\n", "class PermutationsAlt(object):\n",
" if str1 is None or str2 is None:\n", "\n",
" return False\n", " def is_permutation(self, str1, str2):\n",
" if len(str1) != len(str2):\n", " if str1 is None or str2 is None:\n",
" return False\n", " return False\n",
" unique_counts1 = defaultdict(int)\n", " if len(str1) != len(str2):\n",
" unique_counts2 = defaultdict(int)\n", " return False\n",
" for char in str1:\n", " unique_counts1 = defaultdict(int)\n",
" unique_counts1[char] += 1\n", " unique_counts2 = defaultdict(int)\n",
" for char in str2:\n", " for char in str1:\n",
" unique_counts2[char] += 1\n", " unique_counts1[char] += 1\n",
" return unique_counts1 == unique_counts2" " for char in str2:\n",
" unique_counts2[char] += 1\n",
" return unique_counts1 == unique_counts2"
] ]
}, },
{ {
@@ -201,9 +205,11 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestPermutation()\n", " test = TestPermutation()\n",
" test.test_permutation(permutations)\n", " permutations = Permutations()\n",
" test.test_permutation(permutations.is_permutation)\n",
" try:\n", " try:\n",
" test.test_permutation(permutations_alt)\n", " permutations_alt = PermutationsAlt()\n",
" test.test_permutation(permutations_alt.is_permutation)\n",
" except NameError:\n", " except NameError:\n",
" # Alternate solutions are only defined\n", " # Alternate solutions are only defined\n",
" # in the solutions file\n", " # in the solutions file\n",
@@ -225,6 +231,7 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Success: test_permutation\n",
"Success: test_permutation\n" "Success: test_permutation\n"
] ]
} }
@@ -250,7 +257,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.4.3" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@@ -14,9 +14,11 @@ class TestPermutation(object):
def main(): def main():
test = TestPermutation() test = TestPermutation()
test.test_permutation(permutations) permutations = Permutations()
test.test_permutation(permutations.is_permutation)
try: try:
test.test_permutation(permutations_alt) permutations_alt = PermutationsAlt()
test.test_permutation(permutations_alt.is_permutation)
except NameError: except NameError:
# Alternate solutions are only defined # Alternate solutions are only defined
# in the solutions file # in the solutions file