mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
simplify arrays_strings_better_compress
This commit is contained in:
parent
fe227a3fe3
commit
9d09924014
|
@ -62,32 +62,26 @@
|
|||
"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",
|
||||
"* For each char in string starting from index = 1\n",
|
||||
" * If current char is the same as previous, increment count\n",
|
||||
" * Else\n",
|
||||
" * If the count is more than 2\n",
|
||||
" * Append previous char to compressed_string\n",
|
||||
" * Append count to compressed_string\n",
|
||||
" * count = 1\n",
|
||||
" * Else\n",
|
||||
" * If the count is more than 2\n",
|
||||
" * Append last_char to compressed_string\n",
|
||||
" * append count to compressed_string\n",
|
||||
" * count = 1\n",
|
||||
" * last_char = char\n",
|
||||
" * If count is 1\n",
|
||||
" * Append last_char to compressed_string\n",
|
||||
" * count = 1\n",
|
||||
" * last_char = char\n",
|
||||
" * If count is 2\n",
|
||||
" * Append last_char to compressed_string\n",
|
||||
" * Append last_char to compressed_string once more\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",
|
||||
" * Append previous char count times to compressed_string\n",
|
||||
"\n",
|
||||
"* If the count is more than 2\n",
|
||||
" * Append last char of string to compressed_string\n",
|
||||
" * Append count to compressed_string\n",
|
||||
"* Else\n",
|
||||
" * Append last char of string count times to compressed_string\n",
|
||||
"\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",
|
||||
|
@ -104,64 +98,31 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def compress_string(string):\n",
|
||||
" if string is None or len(string) == 0:\n",
|
||||
" if string is None or len(string) < 3:\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",
|
||||
" # New objective:\n",
|
||||
" # Single characters are to be left as is\n",
|
||||
" # Double characters are to be left as are\n",
|
||||
" compressed_string = list()\n",
|
||||
" count = 0\n",
|
||||
" last_char = string[0]\n",
|
||||
" for char in string:\n",
|
||||
" if char == last_char:\n",
|
||||
" \n",
|
||||
" count, compressed_string = 1, \"\"\n",
|
||||
" \n",
|
||||
" # shortcut for repetitive task\n",
|
||||
" append = lambda c, k: c + str(k) if k > 2 else c * k\n",
|
||||
" \n",
|
||||
" for i in range(1, len(string)):\n",
|
||||
" if string[i] == string[i-1]:\n",
|
||||
" count += 1\n",
|
||||
" else:\n",
|
||||
" # Do the old compression tricks only if count exceeds two\n",
|
||||
" if count > 2:\n",
|
||||
" compressed_string.append(last_char)\n",
|
||||
" compressed_string.append(str(count))\n",
|
||||
" count = 1\n",
|
||||
" last_char = char\n",
|
||||
" # If count is either 1 or 2\n",
|
||||
" else:\n",
|
||||
" # If count is 1, leave the char as is\n",
|
||||
" if count == 1:\n",
|
||||
" compressed_string.append(last_char)\n",
|
||||
" count = 1\n",
|
||||
" last_char = char\n",
|
||||
" # If count is 2, append the character twice\n",
|
||||
" else:\n",
|
||||
" compressed_string.append(last_char)\n",
|
||||
" compressed_string.append(last_char)\n",
|
||||
" count = 1\n",
|
||||
" last_char = char\n",
|
||||
" compressed_string.append(last_char)\n",
|
||||
" compressed_string.append(str(count))\n",
|
||||
" compressed_string += append(string[i-1], count)\n",
|
||||
" count = 1\n",
|
||||
"\n",
|
||||
" # Convert the characters in the list to a string\n",
|
||||
" return \"\".join(compressed_string)"
|
||||
" compressed_string += append(string[-1], count)\n",
|
||||
" \n",
|
||||
" if len(compressed_string) < len(string):\n",
|
||||
" return compressed_string\n",
|
||||
" else:\n",
|
||||
" return string"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -201,9 +162,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def split_to_blocks(string):\n",
|
||||
|
@ -240,9 +199,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%writefile test_compress.py\n",
|
||||
|
@ -272,9 +229,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run -i test_compress.py"
|
||||
|
@ -297,9 +252,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user