Polish permutation solution.

This commit is contained in:
Donne Martin 2016-02-10 06:29:50 -05:00
parent 186a192c61
commit 75becf1afb

View File

@ -140,18 +140,15 @@
"from collections import defaultdict\n", "from collections import defaultdict\n",
"\n", "\n",
"\n", "\n",
"def unique_counts(string):\n",
" dict_chars = defaultdict(int)\n",
" for char in string:\n",
" dict_chars[char] += 1\n",
" return dict_chars\n",
"\n",
"\n",
"def permutations_alt(str1, str2):\n", "def permutations_alt(str1, str2):\n",
" if len(str1) != len(str2):\n", " if len(str1) != len(str2):\n",
" return False\n", " return False\n",
" unique_counts1 = unique_counts(str1)\n", " unique_counts2 = defaultdict(int)\n",
" unique_counts2 = unique_counts(str2)\n", " unique_counts2 = defaultdict(int)\n",
" for char in str1:\n",
" unique_counts2[char] += 1\n",
" for char in str2:\n",
" unique_counts2[char] += 1\n",
" return unique_counts1 == unique_counts2" " return unique_counts1 == unique_counts2"
] ]
}, },
@ -218,7 +215,6 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Success: test_permutation\n",
"Success: test_permutation\n" "Success: test_permutation\n"
] ]
} }
@ -230,21 +226,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.10" "version": "3.4.3"
} }
}, },
"nbformat": 4, "nbformat": 4,