Add functional solution to compress string challenge (#103)

This commit is contained in:
Jan Chwiejczak
2016-10-26 13:00:40 +01:00
committed by Donne Martin
parent 0bf56834c0
commit da87bcbfd4

View File

@@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [hashhar](https://github.com/hashhar). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
"This notebook was prepared by [hashhar](https://github.com/hashhar), second solution added by [janhak] (https://github.com/janhak). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
@@ -103,7 +103,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {
"collapsed": false
},
@@ -164,6 +164,72 @@
" return \"\".join(compressed_string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm: Split to blocks and compress\n",
"\n",
"Let us split the string first into blocks of identical characters and then compress it block by block.\n",
"\n",
"* Split the string to blocks\n",
" * For each character in string\n",
" * Add this character to block\n",
" * If the next character is different\n",
" * Return block\n",
" * Erase the content of block\n",
"\n",
"\n",
"* Compress block\n",
" * If block consists of two or fewer characters\n",
" * Return block\n",
" * Else\n",
" * Append length of the block to the first character and return\n",
"\n",
"\n",
"* Compress string\n",
" * Split the string to blocks\n",
" * Compress blocks\n",
" * Join compressed blocks\n",
" * Return result if it is shorter than original string\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: O(n)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def split_to_blocks(string):\n",
" block = ''\n",
" for char, next_char in zip(string, string[1:] + ' '):\n",
" block += char\n",
" if char is not next_char:\n",
" yield block\n",
" block = ''\n",
"\n",
"\n",
"def compress_block(block):\n",
" if len(block) <= 2:\n",
" return block\n",
" else:\n",
" return block[0] + str(len(block))\n",
"\n",
"\n",
"def compress_string(string):\n",
" if string is None or not string:\n",
" return string\n",
" compressed = (compress_block(block) for block in split_to_blocks(string))\n",
" result = ''.join(compressed)\n",
" return result if len(result) < len(string) else string"
]
},
{
"cell_type": "markdown",
"metadata": {},
@@ -173,19 +239,11 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test_compress.py\n"
]
}
],
"outputs": [],
"source": [
"%%writefile test_compress.py\n",
"from nose.tools import assert_equal\n",
@@ -213,19 +271,11 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_compress\n"
]
}
],
"outputs": [],
"source": [
"%run -i test_compress.py"
]