mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish compress solution.
This commit is contained in:
parent
81f0f0c1af
commit
9bf96eabaa
|
@ -60,26 +60,24 @@
|
|||
"source": [
|
||||
"## Algorithm\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",
|
||||
"\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",
|
||||
" * If char is the same as last_char, increment count\n",
|
||||
" * Else\n",
|
||||
" * Append last_char to compressed_string\n",
|
||||
" * append count to compressed_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",
|
||||
"* For each char in string\n",
|
||||
" * If char is the same as last_char, increment count\n",
|
||||
" * Else\n",
|
||||
" * Append last_char and count to compressed_string\n",
|
||||
" * last_char = char\n",
|
||||
" * count = 1\n",
|
||||
"* Append last_char and count to compressed_string\n",
|
||||
"* If the compressed string size is < string size\n",
|
||||
" * Return compressed string\n",
|
||||
"* Else\n",
|
||||
" * Return string\n",
|
||||
"\n",
|
||||
"Complexity:\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",
|
||||
" if string is None or len(string) == 0:\n",
|
||||
" return string\n",
|
||||
"\n",
|
||||
" # Calculate the size of the compressed string\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",
|
||||
" result = ''\n",
|
||||
" prev_char = string[0]\n",
|
||||
" count = 0\n",
|
||||
" last_char = string[0]\n",
|
||||
" for char in string:\n",
|
||||
" if char == last_char:\n",
|
||||
" if char == prev_char:\n",
|
||||
" count += 1\n",
|
||||
" else:\n",
|
||||
" compressed_string.append(last_char)\n",
|
||||
" compressed_string.append(str(count))\n",
|
||||
" result += prev_char + str(count)\n",
|
||||
" prev_char = char\n",
|
||||
" count = 1\n",
|
||||
" 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)"
|
||||
" result += prev_char + str(count)\n",
|
||||
" return result if len(result) < len(string) else string"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -202,21 +180,21 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.4.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
Loading…
Reference in New Issue
Block a user