mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update compress solution so AAAAB becomes A4B.
Previously the solution would yield A4B1, which doesn't have as much compression.
This commit is contained in:
parent
9bf96eabaa
commit
4953e6c114
@ -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'"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -105,7 +105,7 @@
|
|||||||
" if char == prev_char:\n",
|
" if char == prev_char:\n",
|
||||||
" count += 1\n",
|
" count += 1\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" result += prev_char + str(count)\n",
|
" result += prev_char + (str(count) if count > 1 else '')\n",
|
||||||
" prev_char = char\n",
|
" prev_char = char\n",
|
||||||
" count = 1\n",
|
" count = 1\n",
|
||||||
" result += prev_char + str(count)\n",
|
" result += prev_char + str(count)\n",
|
||||||
@ -145,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",
|
||||||
|
@ -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…
x
Reference in New Issue
Block a user