Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified solution for arrays_strings_better_compress #249

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 42 additions & 87 deletions arrays_strings/compress_alt/better_compress_solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
" 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",
" if string is None or len(string) < 3:\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"
]
},
{
Expand Down Expand Up @@ -201,9 +162,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"def split_to_blocks(string):\n",
Expand Down Expand Up @@ -240,9 +199,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"%%writefile test_compress.py\n",
Expand Down Expand Up @@ -272,9 +229,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"%run -i test_compress.py"
Expand All @@ -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
}