Tweaked compress challenge.

This commit is contained in:
Donne Martin 2015-07-05 08:54:49 -04:00
parent 4f5b441b5a
commit d4d7c29c9d

View File

@ -66,9 +66,10 @@
"source": [ "source": [
"## Algorithm: List\n", "## Algorithm: List\n",
"\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", "\n",
"* Calculate the size of the compressed string\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", "* If the compressed string size is >= string size, return string\n",
"* Create compressed_string\n", "* Create compressed_string\n",
" * For each char in string\n", " * For each char in string\n",
@ -84,7 +85,7 @@
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(n)\n", "* Time: O(n)\n",
"* Space: O(n) additional space for the list" "* Space: O(n)"
] ]
}, },
{ {
@ -134,6 +135,8 @@
" last_char = char\n", " last_char = char\n",
" compressed_string.append(last_char)\n", " compressed_string.append(last_char)\n",
" compressed_string.append(str(count))\n", " compressed_string.append(str(count))\n",
" \n",
" # Convert the characters in the list to a string\n",
" return \"\".join(compressed_string)" " return \"\".join(compressed_string)"
] ]
}, },
@ -143,13 +146,20 @@
"source": [ "source": [
"## Algorithm: Byte Array\n", "## Algorithm: Byte Array\n",
"\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", "\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", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(n)\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", " last_char_code = char_code\n",
" compressed_string[pos] = last_char_code\n", " compressed_string[pos] = last_char_code\n",
" compressed_string[pos+1] = ord(str(count))\n", " compressed_string[pos+1] = ord(str(count))\n",
" return compressed_string" " return str(compressed_string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code: Byte Array"
] ]
}, },
{ {