From 92d10aed5ea53d7dde581eaf15af31df629439b8 Mon Sep 17 00:00:00 2001 From: Subham Bhattacharjee Date: Tue, 24 Oct 2017 01:57:30 +0530 Subject: [PATCH] Handled in a Single Function. Tried to handle in a single function. Passes all test cases. --- .../compress/compress_solution.ipynb | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/arrays_strings/compress/compress_solution.ipynb b/arrays_strings/compress/compress_solution.ipynb index fbceb59..e7d43d2 100644 --- a/arrays_strings/compress/compress_solution.ipynb +++ b/arrays_strings/compress/compress_solution.ipynb @@ -102,21 +102,15 @@ " def compress(self, string):\n", " if string is None or not string:\n", " return string\n", - " result = ''\n", - " prev_char = string[0]\n", - " count = 0\n", - " for char in string:\n", - " if char == prev_char:\n", - " count += 1\n", + " count = 1\n", + " encodedString = \"\"\n", + " for x in range(0,len(string)):\n", + " if x + 1 < len(string) and string[x] == string[x + 1]:\n", + " count = count + 1\n", " else:\n", - " result += self._calc_partial_result(prev_char, count)\n", - " prev_char = char\n", - " count = 1\n", - " 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 '')" + " encodedString = encodedString + string[x] + (str(count) if count > 1 else '')\n", + " count = 1\n", + " return encodedString if len(encodedString) < len(string) else string" ] }, {