mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-03 07:58:02 +00:00
Polish compress string (#101)
Fix empty string check, move duplicate code to a method.
This commit is contained in:
@@ -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 '')"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user