mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-03 16:08:02 +00:00
Move string permutation to a class
This commit is contained in:
@@ -84,9 +84,11 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def permutations(str1, str2):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
"class Permutations(object):\n",
|
||||
"\n",
|
||||
" def is_permutation(self, str1, str2):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -130,9 +132,11 @@
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestPermutation()\n",
|
||||
" test.test_permutation(permutations)\n",
|
||||
" permutations = Permutations()\n",
|
||||
" test.test_permutation(permutations.is_permutation)\n",
|
||||
" try:\n",
|
||||
" test.test_permutation(permutations_alt)\n",
|
||||
" permutations_alt = PermutationsAlt()\n",
|
||||
" test.test_permutation(permutations_alt.is_permutation)\n",
|
||||
" except NameError:\n",
|
||||
" # Alternate solutions are only defined\n",
|
||||
" # in the solutions file\n",
|
||||
|
||||
@@ -95,10 +95,12 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def permutations(str1, str2):\n",
|
||||
" if str1 is None or str2 is None:\n",
|
||||
" return False\n",
|
||||
" return sorted(str1) == sorted(str2)"
|
||||
"class Permutations(object):\n",
|
||||
"\n",
|
||||
" def is_permutation(self, str1, str2):\n",
|
||||
" 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",
|
||||
"\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",
|
||||
" unique_counts2 = defaultdict(int)\n",
|
||||
" for char in str1:\n",
|
||||
" unique_counts1[char] += 1\n",
|
||||
" for char in str2:\n",
|
||||
" unique_counts2[char] += 1\n",
|
||||
" return unique_counts1 == unique_counts2"
|
||||
"class PermutationsAlt(object):\n",
|
||||
"\n",
|
||||
" def is_permutation(self, 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",
|
||||
" unique_counts2 = defaultdict(int)\n",
|
||||
" for char in str1:\n",
|
||||
" unique_counts1[char] += 1\n",
|
||||
" for char in str2:\n",
|
||||
" unique_counts2[char] += 1\n",
|
||||
" return unique_counts1 == unique_counts2"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -201,9 +205,11 @@
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestPermutation()\n",
|
||||
" test.test_permutation(permutations)\n",
|
||||
" permutations = Permutations()\n",
|
||||
" test.test_permutation(permutations.is_permutation)\n",
|
||||
" try:\n",
|
||||
" test.test_permutation(permutations_alt)\n",
|
||||
" permutations_alt = PermutationsAlt()\n",
|
||||
" test.test_permutation(permutations_alt.is_permutation)\n",
|
||||
" except NameError:\n",
|
||||
" # Alternate solutions are only defined\n",
|
||||
" # in the solutions file\n",
|
||||
@@ -225,6 +231,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Success: test_permutation\n",
|
||||
"Success: test_permutation\n"
|
||||
]
|
||||
}
|
||||
@@ -250,7 +257,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.4.3"
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -14,9 +14,11 @@ class TestPermutation(object):
|
||||
|
||||
def main():
|
||||
test = TestPermutation()
|
||||
test.test_permutation(permutations)
|
||||
permutations = Permutations()
|
||||
test.test_permutation(permutations.is_permutation)
|
||||
try:
|
||||
test.test_permutation(permutations_alt)
|
||||
permutations_alt = PermutationsAlt()
|
||||
test.test_permutation(permutations_alt.is_permutation)
|
||||
except NameError:
|
||||
# Alternate solutions are only defined
|
||||
# in the solutions file
|
||||
|
||||
Reference in New Issue
Block a user