From 73924c279a1ce8c3d73a3df2e5796140b649692a Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Mon, 6 Jul 2015 06:30:00 -0400 Subject: [PATCH] Removed bytearray solution, needs some tweaks to make it work with Python 3. --- .../compress/compress_challenge.ipynb | 7 -- .../compress/compress_solution.ipynb | 89 ++----------------- arrays_strings/compress/test_compress.py | 6 -- 3 files changed, 6 insertions(+), 96 deletions(-) diff --git a/arrays_strings/compress/compress_challenge.ipynb b/arrays_strings/compress/compress_challenge.ipynb index ad811f7..11f3ade 100644 --- a/arrays_strings/compress/compress_challenge.ipynb +++ b/arrays_strings/compress/compress_challenge.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* Can we assume the string is ASCII?\n", " * Yes\n", " * Note: Unicode strings could require special handling depending on your language\n", @@ -127,12 +126,6 @@ "def main():\n", " test = TestCompress()\n", " test.test_compress(compress_string)\n", - " try:\n", - " test.test_compress(compress_string_alt)\n", - " except NameError:\n", - " # Alternate solutions are only defined\n", - " # in the solutions file\n", - " pass\n", "\n", "if __name__ == '__main__':\n", " main()" diff --git a/arrays_strings/compress/compress_solution.ipynb b/arrays_strings/compress/compress_solution.ipynb index 2e81157..d08e18e 100644 --- a/arrays_strings/compress/compress_solution.ipynb +++ b/arrays_strings/compress/compress_solution.ipynb @@ -22,10 +22,8 @@ "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", - "* [Algorithm: List](#Algorithm:-List)\n", - "* [Code: List](#Code:-List)\n", - "* [Algorithm: Byte Array](#Algorithm:-Byte-Array)\n", - "* [Code: Byte array](#Code:-Byte-Array)\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)\n", "* [Unit Test](#Unit-Test)" ] }, @@ -35,7 +33,6 @@ "source": [ "## Constraints\n", "\n", - "* Can we assume the string is ASCII?\n", " * Yes\n", " * Note: Unicode strings could require special handling depending on your language\n", @@ -63,7 +60,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Algorithm: List\n", + "## Algorithm\n", "\n", "Since Python strings are immutable, we'll use a list of characters to build the compressed string representation. We'll then convert the list to a string.\n", "\n", @@ -91,7 +88,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Code: List" + "## Code" ] }, { @@ -139,73 +136,6 @@ " return \"\".join(compressed_string)" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Algorithm: Byte Array\n", - "\n", - "As a bonus solution, we can also solve this problem with a byte array.\n", - "\n", - "The byte array algorithm similar when using a list, except we will need to work with the bytearray's character codes (using the function ord) instead of the characters as when we implemented this solution with a list.\n", - "\n", - "Complexity:\n", - "* Time: O(n)\n", - "* Space: O(n)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Code: Byte Array" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def compress_string_alt(string):\n", - " if string is None or len(string) == 0:\n", - " return string\n", - " \n", - " # Calculate the size of the compressed string\n", - " size = 0\n", - " last_char_code = string[0]\n", - " for char_code in string:\n", - " if char_code != last_char_code:\n", - " size += 2\n", - " last_char_code = char_code\n", - " size += 2\n", - " \n", - " # If the compressed string size is greater than \n", - " # or equal to string size, return string \n", - " if size >= len(string):\n", - " return string\n", - " \n", - " # Create compressed_string\n", - " compressed_string = bytearray(size)\n", - " pos = 0\n", - " count = 0\n", - " last_char_code = string[0]\n", - " for char_code in string:\n", - " if char_code == last_char_code:\n", - " count += 1\n", - " else:\n", - " compressed_string[pos] = last_char_code\n", - " compressed_string[pos+1] = ord(str(count))\n", - " pos += 2\n", - " count = 1\n", - " last_char_code = char_code\n", - " compressed_string[pos] = last_char_code\n", - " compressed_string[pos+1] = ord(str(count))\n", - " return str(compressed_string)" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -215,7 +145,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -245,12 +175,6 @@ "def main():\n", " test = TestCompress()\n", " test.test_compress(compress_string)\n", - " try:\n", - " test.test_compress(compress_string_alt)\n", - " except NameError:\n", - " # Alternate solutions are only defined\n", - " # in the solutions file\n", - " pass\n", "\n", "if __name__ == '__main__':\n", " main()" @@ -258,7 +182,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -267,7 +191,6 @@ "name": "stdout", "output_type": "stream", "text": [ - "Success: test_compress\n", "Success: test_compress\n" ] } diff --git a/arrays_strings/compress/test_compress.py b/arrays_strings/compress/test_compress.py index fef9e62..4865a33 100644 --- a/arrays_strings/compress/test_compress.py +++ b/arrays_strings/compress/test_compress.py @@ -13,12 +13,6 @@ class TestCompress(object): def main(): test = TestCompress() test.test_compress(compress_string) - try: - test.test_compress(compress_string_alt) - except NameError: - # Alternate solutions are only defined - # in the solutions file - pass if __name__ == '__main__': main() \ No newline at end of file