mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish compress string (#101)
Fix empty string check, move duplicate code to a method.
This commit is contained in:
parent
ff9181fe92
commit
0f49f67a72
|
@ -100,7 +100,7 @@
|
|||
"class CompressString(object):\n",
|
||||
"\n",
|
||||
" def compress(self, string):\n",
|
||||
" if string is None or len(string) == 0:\n",
|
||||
" if string is None or not string:\n",
|
||||
" return string\n",
|
||||
" result = ''\n",
|
||||
" prev_char = string[0]\n",
|
||||
|
@ -109,11 +109,14 @@
|
|||
" if char == prev_char:\n",
|
||||
" count += 1\n",
|
||||
" else:\n",
|
||||
" result += prev_char + (str(count) if count > 1 else '')\n",
|
||||
" result += self._calc_partial_result(prev_char, count)\n",
|
||||
" prev_char = char\n",
|
||||
" count = 1\n",
|
||||
" result += prev_char + (str(count) if count > 1 else '')\n",
|
||||
" return result if len(result) < len(string) else string"
|
||||
" result += self._calc_partial_result(prev_char, count)\n",
|
||||
" return result if len(result) < len(string) else string\n",
|
||||
"\n",
|
||||
" def _calc_partial_result(self, prev_char, count):\n",
|
||||
" return prev_char + (str(count) if count > 1 else '')"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user