Moved pythonic solution after the initial python solution and prior to the C solution.

This commit is contained in:
Donne Martin 2015-06-23 22:38:55 -04:00
parent 7ee5a4da8f
commit 7ec208b0eb

View File

@ -17,9 +17,9 @@
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Pythonic-Code](#Pythonic-Code)\n",
"* [C Algorithm](C-Algorithm)\n",
"* [C Code]()\n",
"* [Pythonic-Code](#Pythonic-Code)"
"* [C Code]()"
]
},
{
@ -47,7 +47,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"collapsed": false
},
@ -92,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {
"collapsed": false
},
@ -111,6 +111,37 @@
"run_tests(reverse_string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pythonic-Code\n",
"\n",
"This question has an artificial constraint that prevented the use of the slice operator and the reversed method. For completeness, the solutions for these are provided below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def reverse_string_alt(string):\n",
" if string is None:\n",
" return None\n",
" return string[::-1]\n",
"\n",
"def reverse_string_alt2(string):\n",
" if string is None:\n",
" return None \n",
" return ''.join(reversed(string))\n",
"\n",
"run_tests(reverse_string_alt)\n",
"run_tests(reverse_string_alt2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -189,37 +220,6 @@
" return 0;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pythonic-Code\n",
"\n",
"This question has an artificial constraint that prevented the use of the slice operator and the reversed method. For completeness, the solutions for these are provided below."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def reverse_string_alt(string):\n",
" if string is None:\n",
" return None\n",
" return string[::-1]\n",
"\n",
"def reverse_string_alt2(string):\n",
" if string is None:\n",
" return None \n",
" return ''.join(reversed(string))\n",
"\n",
"run_tests(reverse_string_alt)\n",
"run_tests(reverse_string_alt2)"
]
}
],
"metadata": {