Polish compress string (#101)

Fix empty string check, move duplicate code to a method.
This commit is contained in:
Donne Martin 2016-10-12 07:28:12 -04:00 committed by GitHub
parent ff9181fe92
commit 0f49f67a72

View File

@ -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 '')"
]
},
{