From d4d7c29c9d0147d68149f0f924815f0c923b0c5d Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sun, 5 Jul 2015 08:54:49 -0400 Subject: [PATCH] Tweaked compress challenge. --- .../compress/compress_solution.ipynb | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/arrays_strings/compress/compress_solution.ipynb b/arrays_strings/compress/compress_solution.ipynb index 0444448..1b80285 100644 --- a/arrays_strings/compress/compress_solution.ipynb +++ b/arrays_strings/compress/compress_solution.ipynb @@ -66,9 +66,10 @@ "source": [ "## Algorithm: List\n", "\n", - "Since Python strings are immutable, we'll use a list of characters instead to exercise string manipulation as you would get with a C string. We'll convert the list to a string at the end of the algorithm.\n", + "Since Python strings are immutable, we'll use a list of characters to build the compressed string representation. We'll then convert the list to a string.\n", "\n", "* Calculate the size of the compressed string\n", + " * Note the constraint about compressing only if it saves space\n", "* If the compressed string size is >= string size, return string\n", "* Create compressed_string\n", " * For each char in string\n", @@ -84,7 +85,7 @@ "\n", "Complexity:\n", "* Time: O(n)\n", - "* Space: O(n) additional space for the list" + "* Space: O(n)" ] }, { @@ -134,6 +135,8 @@ " last_char = char\n", " compressed_string.append(last_char)\n", " compressed_string.append(str(count))\n", + " \n", + " # Convert the characters in the list to a string\n", " return \"\".join(compressed_string)" ] }, @@ -143,13 +146,20 @@ "source": [ "## Algorithm: Byte Array\n", "\n", - "As a bonus solution, we can solve this problem with a byte array.\n", + "As a bonus solution, we can also solve this problem with a byte array.\n", "\n", - "The byte array algorithm similar when using a list, except we will need to work with the bytearray's character codes (using the function ord) instead of the characters as we did above when we implemented this solution with a list.\n", + "The byte array algorithm similar when using a list, except we will need to work with the bytearray's character codes (using the function ord) instead of the characters as when we implemented this solution with a list.\n", "\n", "Complexity:\n", "* Time: O(n)\n", - "* Space: O(m) where m is the size of the compressed bytearray" + "* Space: O(n)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code: Byte Array" ] }, { @@ -194,14 +204,7 @@ " last_char_code = char_code\n", " compressed_string[pos] = last_char_code\n", " compressed_string[pos+1] = ord(str(count))\n", - " return compressed_string" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Code: Byte Array" + " return str(compressed_string)" ] }, {