From e22f7b8d208c686d8923db776a4730d67342a6d5 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 2 May 2015 17:13:10 -0400 Subject: [PATCH] Added notebook index. Added note about using additional data structures for the Pythonic solution. Minor cleanup. --- arrays-strings/reverse_string.ipynb | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/arrays-strings/reverse_string.ipynb b/arrays-strings/reverse_string.ipynb index 6019830..3f009d3 100644 --- a/arrays-strings/reverse_string.ipynb +++ b/arrays-strings/reverse_string.ipynb @@ -4,7 +4,13 @@ "cell_type": "markdown", "metadata": {}, "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", "\n", "* NULL input\n", - "* \"\" -> \"\"\n", - "* \"foo bar\" -> \"rab oof\"" + "* '' -> ''\n", + "* 'foo bar' -> 'rab oof'" ] }, { @@ -36,7 +42,7 @@ "source": [ "## Algorithm\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", "* j is a pointer to the last char\n", "\n", @@ -46,8 +52,8 @@ " * swap i and j\n", "\n", "Complexity:\n", - "* Time: O(n).\n", - "* Space: In-place.\n", + "* Time: O(n)\n", + "* Space: In-place\n", "\n", "Note:\n", "* Instead of using i, you can use str instead, although this might not be as intuitive." @@ -111,7 +117,9 @@ "cell_type": "markdown", "metadata": {}, "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", "\n", "def reverse_string_alt2(string):\n", - " return \"\".join(reversed(string))" + " return ''.join(reversed(string))" ] } ],