mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-03 16:08:02 +00:00
Polish compress solution.
This commit is contained in:
@@ -60,26 +60,24 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Algorithm\n",
|
"## Algorithm\n",
|
||||||
"\n",
|
"\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",
|
"* For each char in string\n",
|
||||||
"\n",
|
" * If char is the same as last_char, increment count\n",
|
||||||
"* Calculate the size of the compressed string\n",
|
" * Else\n",
|
||||||
" * Note the constraint about compressing only if it saves space\n",
|
" * Append last_char and count to compressed_string\n",
|
||||||
"* If the compressed string size is >= string size, return string\n",
|
" * last_char = char\n",
|
||||||
"* Create compressed_string\n",
|
" * count = 1\n",
|
||||||
" * For each char in string\n",
|
"* Append last_char and count to compressed_string\n",
|
||||||
" * If char is the same as last_char, increment count\n",
|
"* If the compressed string size is < string size\n",
|
||||||
" * Else\n",
|
" * Return compressed string\n",
|
||||||
" * Append last_char to compressed_string\n",
|
"* Else\n",
|
||||||
" * append count to compressed_string\n",
|
" * Return string\n",
|
||||||
" * count = 1\n",
|
|
||||||
" * last_char = char\n",
|
|
||||||
" * Append last_char to compressed_string\n",
|
|
||||||
" * Append count to compressed_string\n",
|
|
||||||
" * Return compressed_string\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(n)\n",
|
"* Time: O(n)\n",
|
||||||
"* Space: O(n)"
|
"* Space: O(n)\n",
|
||||||
|
"\n",
|
||||||
|
"Complexity Note:\n",
|
||||||
|
"* Although strings are immutable in Python, appending to strings is optimized in CPython so that it now runs in O(n) and extends the string in-place. Refer to this [Stack Overflow post](http://stackoverflow.com/a/4435752)."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -100,38 +98,18 @@
|
|||||||
"def compress_string(string):\n",
|
"def compress_string(string):\n",
|
||||||
" if string is None or len(string) == 0:\n",
|
" if string is None or len(string) == 0:\n",
|
||||||
" return string\n",
|
" return string\n",
|
||||||
"\n",
|
" result = ''\n",
|
||||||
" # Calculate the size of the compressed string\n",
|
" prev_char = string[0]\n",
|
||||||
" size = 0\n",
|
|
||||||
" last_char = string[0]\n",
|
|
||||||
" for char in string:\n",
|
|
||||||
" if char != last_char:\n",
|
|
||||||
" size += 2\n",
|
|
||||||
" last_char = char\n",
|
|
||||||
" size += 2\n",
|
|
||||||
"\n",
|
|
||||||
" # If the compressed string size is greater than\n",
|
|
||||||
" # or equal to string size, return original string\n",
|
|
||||||
" if size >= len(string):\n",
|
|
||||||
" return string\n",
|
|
||||||
"\n",
|
|
||||||
" # Create compressed_string\n",
|
|
||||||
" compressed_string = list()\n",
|
|
||||||
" count = 0\n",
|
" count = 0\n",
|
||||||
" last_char = string[0]\n",
|
|
||||||
" for char in string:\n",
|
" for char in string:\n",
|
||||||
" if char == last_char:\n",
|
" if char == prev_char:\n",
|
||||||
" count += 1\n",
|
" count += 1\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" compressed_string.append(last_char)\n",
|
" result += prev_char + str(count)\n",
|
||||||
" compressed_string.append(str(count))\n",
|
" prev_char = char\n",
|
||||||
" count = 1\n",
|
" count = 1\n",
|
||||||
" last_char = char\n",
|
" result += prev_char + str(count)\n",
|
||||||
" compressed_string.append(last_char)\n",
|
" return result if len(result) < len(string) else string"
|
||||||
" compressed_string.append(str(count))\n",
|
|
||||||
"\n",
|
|
||||||
" # Convert the characters in the list to a string\n",
|
|
||||||
" return \"\".join(compressed_string)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -202,21 +180,21 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.4.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
Reference in New Issue
Block a user