Added notebook index. Added note about using additional data structures for the Pythonic solution. Minor cleanup.

This commit is contained in:
Donne Martin
2015-05-02 17:13:10 -04:00
parent 7826fea184
commit e22f7b8d20

View File

@@ -4,7 +4,13 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Problem: Implement the function void Reverse(char* str)" "## Problem: Implement the function void Reverse(char* str)\n",
"\n",
"* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Pythonic-Code](#Pythonic-Code)"
] ]
}, },
{ {
@@ -26,8 +32,8 @@
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* NULL input\n", "* NULL input\n",
"* \"\" -> \"\"\n", "* '' -> ''\n",
"* \"foo bar\" -> \"rab oof\"" "* 'foo bar' -> 'rab oof'"
] ]
}, },
{ {
@@ -36,7 +42,7 @@
"source": [ "source": [
"## Algorithm\n", "## Algorithm\n",
"\n", "\n",
"We'll want to keep two pointers\n", "We'll want to keep two pointers:\n",
"* i is a pointer to the first char\n", "* i is a pointer to the first char\n",
"* j is a pointer to the last char\n", "* j is a pointer to the last char\n",
"\n", "\n",
@@ -46,8 +52,8 @@
" * swap i and j\n", " * swap i and j\n",
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(n).\n", "* Time: O(n)\n",
"* Space: In-place.\n", "* Space: In-place\n",
"\n", "\n",
"Note:\n", "Note:\n",
"* Instead of using i, you can use str instead, although this might not be as intuitive." "* Instead of using i, you can use str instead, although this might not be as intuitive."
@@ -111,7 +117,9 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Pythonic Solution(s)" "## Pythonic Code\n",
"\n",
"The following code is Pythonic, but requires using additional data structures as Python strings are immutable. You could use a bytearray or a list instead of a string to simulate manipulating an array of characters."
] ]
}, },
{ {
@@ -126,7 +134,7 @@
" return string[::-1]\n", " return string[::-1]\n",
"\n", "\n",
"def reverse_string_alt2(string):\n", "def reverse_string_alt2(string):\n",
" return \"\".join(reversed(string))" " return ''.join(reversed(string))"
] ]
} }
], ],