From aa887db2e0131360a52707da8c61cc856cde0732 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 13 Aug 2016 06:37:48 -0400 Subject: [PATCH] Move string compress to a class, update test --- .../compress/compress_challenge.ipynb | 13 ++++--- .../compress/compress_solution.ipynb | 35 ++++++++++--------- arrays_strings/compress/test_compress.py | 3 +- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/arrays_strings/compress/compress_challenge.ipynb b/arrays_strings/compress/compress_challenge.ipynb index b3ecd66..2eb0f8f 100644 --- a/arrays_strings/compress/compress_challenge.ipynb +++ b/arrays_strings/compress/compress_challenge.ipynb @@ -81,9 +81,11 @@ }, "outputs": [], "source": [ - "def compress_string(string):\n", - " # TODO: Implement me\n", - " pass" + "class CompressString(object):\n", + "\n", + " def compress(self, string):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -120,13 +122,14 @@ " assert_equal(func(None), None)\n", " assert_equal(func(''), '')\n", " assert_equal(func('AABBCC'), 'AABBCC')\n", - " assert_equal(func('AAABCCDDDD'), 'A3BC2D4')\n", + " assert_equal(func('AAABCCDDDDE'), 'A3BC2D4E')\n", " print('Success: test_compress')\n", "\n", "\n", "def main():\n", " test = TestCompress()\n", - " test.test_compress(compress_string)\n", + " compress_string = CompressString()\n", + " test.test_compress(compress_string.compress)\n", "\n", "\n", "if __name__ == '__main__':\n", diff --git a/arrays_strings/compress/compress_solution.ipynb b/arrays_strings/compress/compress_solution.ipynb index a19e4ab..781d949 100644 --- a/arrays_strings/compress/compress_solution.ipynb +++ b/arrays_strings/compress/compress_solution.ipynb @@ -97,21 +97,23 @@ }, "outputs": [], "source": [ - "def compress_string(string):\n", - " if string is None or len(string) == 0:\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", - " else:\n", - " result += prev_char + (str(count) if count > 1 else '')\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" + "class CompressString(object):\n", + "\n", + " def compress(self, string):\n", + " if string is None or len(string) == 0:\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", + " else:\n", + " result += prev_char + (str(count) if count > 1 else '')\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" ] }, { @@ -153,7 +155,8 @@ "\n", "def main():\n", " test = TestCompress()\n", - " test.test_compress(compress_string)\n", + " compress_string = CompressString()\n", + " test.test_compress(compress_string.compress)\n", "\n", "\n", "if __name__ == '__main__':\n", diff --git a/arrays_strings/compress/test_compress.py b/arrays_strings/compress/test_compress.py index 0b1350e..e0828d6 100644 --- a/arrays_strings/compress/test_compress.py +++ b/arrays_strings/compress/test_compress.py @@ -13,7 +13,8 @@ class TestCompress(object): def main(): test = TestCompress() - test.test_compress(compress_string) + compress_string = CompressString() + test.test_compress(compress_string.compress) if __name__ == '__main__':