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:
Donne Martin 2016-02-09 06:52:32 -05:00
parent 9bf96eabaa
commit 4953e6c114
2 changed files with 5 additions and 5 deletions

View File

@ -18,7 +18,7 @@
"cell_type": "markdown",
"metadata": {},
"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",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
@ -51,7 +51,7 @@
"* None -> None\n",
"* '' -> ''\n",
"* 'AABBCC' -> 'AABBCC'\n",
"* 'AAABCCDDDD' -> 'A3B1C2D4'"
"* 'AAABCCDDDD' -> 'A3BC2D4'"
]
},
{
@ -105,7 +105,7 @@
" if char == prev_char:\n",
" count += 1\n",
" else:\n",
" result += prev_char + str(count)\n",
" result += prev_char + (str(count) if count > 1 else '')\n",
" prev_char = char\n",
" count = 1\n",
" result += prev_char + str(count)\n",
@ -145,7 +145,7 @@
" assert_equal(func(None), None)\n",
" assert_equal(func(''), '')\n",
" assert_equal(func('AABBCC'), 'AABBCC')\n",
" assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')\n",
" assert_equal(func('AAABCCDDDD'), 'A3BC2D4')\n",
" print('Success: test_compress')\n",
"\n",
"\n",

View File

@ -7,7 +7,7 @@ class TestCompress(object):
assert_equal(func(None), None)
assert_equal(func(''), '')
assert_equal(func('AABBCC'), 'AABBCC')
assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')
assert_equal(func('AAABCCDDDD'), 'A3BC2D4')
print('Success: test_compress')