From 0f49f67a72d60428acbf7ffa23dfdf8f00246820 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Wed, 12 Oct 2016 07:28:12 -0400 Subject: [PATCH] Polish compress string (#101) Fix empty string check, move duplicate code to a method. --- arrays_strings/compress/compress_solution.ipynb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/arrays_strings/compress/compress_solution.ipynb b/arrays_strings/compress/compress_solution.ipynb index 781d949..b61ecf4 100644 --- a/arrays_strings/compress/compress_solution.ipynb +++ b/arrays_strings/compress/compress_solution.ipynb @@ -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 '')" ] }, {