mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Merge pull request #44 from donnemartin/develop
Polish compress string solution
This commit is contained in:
commit
b53d7ba9ef
|
@ -18,7 +18,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3B1C2D4'. Only compress the string if it saves space.\n",
|
"## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3BC2D4'. Only compress the string if it saves space.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* [Constraints](#Constraints)\n",
|
"* [Constraints](#Constraints)\n",
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
|
@ -51,7 +51,7 @@
|
||||||
"* None -> None\n",
|
"* None -> None\n",
|
||||||
"* '' -> ''\n",
|
"* '' -> ''\n",
|
||||||
"* 'AABBCC' -> 'AABBCC'\n",
|
"* 'AABBCC' -> 'AABBCC'\n",
|
||||||
"* 'AAABCCDDDD' -> 'A3B1C2D4'"
|
"* 'AAABCCDDDD' -> 'A3BC2D4'"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -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) if count > 1 else '')\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)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -167,7 +145,7 @@
|
||||||
" assert_equal(func(None), None)\n",
|
" assert_equal(func(None), None)\n",
|
||||||
" assert_equal(func(''), '')\n",
|
" assert_equal(func(''), '')\n",
|
||||||
" assert_equal(func('AABBCC'), 'AABBCC')\n",
|
" assert_equal(func('AABBCC'), 'AABBCC')\n",
|
||||||
" assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')\n",
|
" assert_equal(func('AAABCCDDDD'), 'A3BC2D4')\n",
|
||||||
" print('Success: test_compress')\n",
|
" print('Success: test_compress')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -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,
|
||||||
|
|
|
@ -7,7 +7,7 @@ class TestCompress(object):
|
||||||
assert_equal(func(None), None)
|
assert_equal(func(None), None)
|
||||||
assert_equal(func(''), '')
|
assert_equal(func(''), '')
|
||||||
assert_equal(func('AABBCC'), 'AABBCC')
|
assert_equal(func('AABBCC'), 'AABBCC')
|
||||||
assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')
|
assert_equal(func('AAABCCDDDD'), 'A3BC2D4')
|
||||||
print('Success: test_compress')
|
print('Success: test_compress')
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user