From ef77603f9bda119eca9d25a74b232856a2d2937f Mon Sep 17 00:00:00 2001 From: Ashhar Hasan Date: Fri, 30 Oct 2015 03:06:17 +0530 Subject: [PATCH 1/7] Improved compression by only appending count if the count is more than 2 --- .../compress/compress_solution.ipynb | 128 ++++++++++++------ arrays_strings/compress/test_compress.py | 2 +- 2 files changed, 84 insertions(+), 46 deletions(-) diff --git a/arrays_strings/compress/compress_solution.ipynb b/arrays_strings/compress/compress_solution.ipynb index aa11798..c1c5afc 100644 --- a/arrays_strings/compress/compress_solution.ipynb +++ b/arrays_strings/compress/compress_solution.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3B1C2D4'. Only compress the string if it saves space.\n", + "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3BCCD4'. Only compress the string if it saves space.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", @@ -51,7 +51,7 @@ "* None -> None\n", "* '' -> ''\n", "* 'AABBCC' -> 'AABBCC'\n", - "* 'AAABCCDDDD' -> 'A3B1C2D4'" + "* 'AAABCCDDDD' -> 'A3BCCD4'" ] }, { @@ -69,10 +69,20 @@ " * For each char in string\n", " * If char is the same as last_char, increment count\n", " * Else\n", - " * Append last_char to compressed_string\n", - " * append count to compressed_string\n", - " * count = 1\n", - " * last_char = char\n", + " * If the count is more than 2\n", + " * Append last_char to compressed_string\n", + " * append count to compressed_string\n", + " * count = 1\n", + " * last_char = char\n", + " * If count is 1\n", + " * Append last_char to compressed_string\n", + " * count = 1\n", + " * last_char = char\n", + " * If count is 2\n", + " * Append last_char to compressed_string\n", + " * Append last_char to compressed_string once more\n", + " * count = 1\n", + " * last_char = char\n", " * Append last_char to compressed_string\n", " * Append count to compressed_string\n", " * Return compressed_string\n", @@ -91,47 +101,66 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def compress_string(string):\n", - " if string is None or len(string) == 0:\n", - " return string\n", + "\tif string is None or len(string) == 0:\n", + "\t\treturn string\n", "\n", - " # Calculate the size of the compressed string\n", - " size = 0\n", - " last_char = string[0]\n", - " for char in string:\n", - " if char != last_char:\n", - " size += 2\n", - " last_char = char\n", - " size += 2\n", + "\t# Calculate the size of the compressed string\n", + "\tsize = 0\n", + "\tlast_char = string[0]\n", + "\tfor char in string:\n", + "\t\tif char != last_char:\n", + "\t\t\tsize += 2\n", + "\t\t\tlast_char = char\n", + "\tsize += 2\n", "\n", - " # If the compressed string size is greater than\n", - " # or equal to string size, return original string\n", - " if size >= len(string):\n", - " return string\n", + "\t# If the compressed string size is greater than\n", + "\t# or equal to string size, return original string\n", + "\tif size >= len(string):\n", + "\t\treturn string\n", "\n", - " # Create compressed_string\n", - " compressed_string = list()\n", - " count = 0\n", - " last_char = string[0]\n", - " for char in string:\n", - " if char == last_char:\n", - " count += 1\n", - " else:\n", - " compressed_string.append(last_char)\n", - " compressed_string.append(str(count))\n", - " count = 1\n", - " last_char = char\n", - " compressed_string.append(last_char)\n", - " compressed_string.append(str(count))\n", + "\t# Create compressed_string\n", + "\t# New objective:\n", + "\t\t# Single characters are to be left as is\n", + "\t\t# Double characters are to be left as are\n", + "\tcompressed_string = list()\n", + "\tcount = 0\n", + "\tlast_char = string[0]\n", + "\tfor char in string:\n", + "\t\tif char == last_char:\n", + "\t\t\tcount += 1\n", + "\t\telse:\n", + "\t\t\t# Do the old compression tricks only if count exceeds two\n", + "\t\t\tif count > 2:\n", + "\t\t\t\tcompressed_string.append(last_char)\n", + "\t\t\t\tcompressed_string.append(str(count))\n", + "\t\t\t\tcount = 1\n", + "\t\t\t\tlast_char = char\n", + "\t\t\t# If count is either 1 or 2 (or 0?)\n", + "\t\t\t# FIXME: CAN COUNT BE 0?\n", + "\t\t\telse:\n", + "\t\t\t\t# If count is 1, leave the char as is\n", + "\t\t\t\tif count == 1:\n", + "\t\t\t\t\tcompressed_string.append(last_char)\n", + "\t\t\t\t\tcount = 1\n", + "\t\t\t\t\tlast_char = char\n", + "\t\t\t\t# If count is 2, append the character twice\n", + "\t\t\t\telse:\n", + "\t\t\t\t\tcompressed_string.append(last_char)\n", + "\t\t\t\t\tcompressed_string.append(last_char)\n", + "\t\t\t\t\tcount = 1\n", + "\t\t\t\t\tlast_char = char\n", + "\tcompressed_string.append(last_char)\n", + "\tcompressed_string.append(str(count))\n", "\n", - " # Convert the characters in the list to a string\n", - " return \"\".join(compressed_string)" + "\t# Convert the characters in the list to a string\n", + "\treturn \"\".join(compressed_string)" ] }, { @@ -143,7 +172,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 15, "metadata": { "collapsed": false }, @@ -167,7 +196,7 @@ " assert_equal(func(None), None)\n", " assert_equal(func(''), '')\n", " assert_equal(func('AABBCC'), 'AABBCC')\n", - " assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')\n", + " assert_equal(func('AAABCCDDDD'), 'A3BCCD4')\n", " print('Success: test_compress')\n", "\n", "\n", @@ -182,7 +211,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 16, "metadata": { "collapsed": false }, @@ -198,25 +227,34 @@ "source": [ "%run -i test_compress.py" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.4.3" } }, "nbformat": 4, diff --git a/arrays_strings/compress/test_compress.py b/arrays_strings/compress/test_compress.py index 7ce5142..c527360 100644 --- a/arrays_strings/compress/test_compress.py +++ b/arrays_strings/compress/test_compress.py @@ -7,7 +7,7 @@ class TestCompress(object): assert_equal(func(None), None) assert_equal(func(''), '') assert_equal(func('AABBCC'), 'AABBCC') - assert_equal(func('AAABCCDDDD'), 'A3B1C2D4') + assert_equal(func('AAABCCDDDD'), 'A3BCCD4') print('Success: test_compress') From eb44aae3f5aa36a5631cecc01eae3e8fd19d1728 Mon Sep 17 00:00:00 2001 From: Ashhar Hasan Date: Fri, 30 Oct 2015 03:15:27 +0530 Subject: [PATCH 2/7] Added the same changes to test cases in the challenge notebook --- arrays_strings/compress/compress_challenge.ipynb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/arrays_strings/compress/compress_challenge.ipynb b/arrays_strings/compress/compress_challenge.ipynb index fb17544..b1f3afe 100644 --- a/arrays_strings/compress/compress_challenge.ipynb +++ b/arrays_strings/compress/compress_challenge.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3B1C2D4'. Only compress the string if it saves space.\n", + "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3BCCD4'. Only compress the string if it saves space.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", @@ -52,7 +52,7 @@ "* None -> None\n", "* '' -> ''\n", "* 'AABBCC' -> 'AABBCC'\n", - "* 'AAABCCDDDD' -> 'A3B1C2D4'" + "* 'AAABCCDDDD' -> 'A3BCCD4'" ] }, { @@ -118,7 +118,7 @@ " assert_equal(func(None), None)\n", " assert_equal(func(''), '')\n", " assert_equal(func('AABBCC'), 'AABBCC')\n", - " assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')\n", + " assert_equal(func('AAABCCDDDD'), 'A3BCCD4')\n", " print('Success: test_compress')\n", "\n", "\n", @@ -143,21 +143,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.4.3" } }, "nbformat": 4, From 206841bc5d795011cad57ecc4e742d4e8a51001d Mon Sep 17 00:00:00 2001 From: Ashhar Hasan Date: Fri, 30 Oct 2015 03:24:56 +0530 Subject: [PATCH 3/7] New test case to illustrate the improvements --- arrays_strings/compress/compress_solution.ipynb | 7 ++++--- arrays_strings/compress/test_compress.py | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/arrays_strings/compress/compress_solution.ipynb b/arrays_strings/compress/compress_solution.ipynb index c1c5afc..3770c03 100644 --- a/arrays_strings/compress/compress_solution.ipynb +++ b/arrays_strings/compress/compress_solution.ipynb @@ -101,7 +101,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -172,7 +172,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -197,6 +197,7 @@ " assert_equal(func(''), '')\n", " assert_equal(func('AABBCC'), 'AABBCC')\n", " assert_equal(func('AAABCCDDDD'), 'A3BCCD4')\n", + " assert_equal(func('aaBCCEFFFFKKMMMMMMP taaammanlaarrrr seeeeeeeeek tooo'), 'aaBCCEF4KKM6P ta3mmanlaar4 se9k to3')\n", " print('Success: test_compress')\n", "\n", "\n", @@ -211,7 +212,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 7, "metadata": { "collapsed": false }, diff --git a/arrays_strings/compress/test_compress.py b/arrays_strings/compress/test_compress.py index c527360..b15dde4 100644 --- a/arrays_strings/compress/test_compress.py +++ b/arrays_strings/compress/test_compress.py @@ -8,6 +8,7 @@ class TestCompress(object): assert_equal(func(''), '') assert_equal(func('AABBCC'), 'AABBCC') assert_equal(func('AAABCCDDDD'), 'A3BCCD4') + assert_equal(func('aaBCCEFFFFKKMMMMMMP taaammanlaarrrr seeeeeeeeek tooo'), 'aaBCCEF4KKM6P ta3mmanlaar4 se9k to3') print('Success: test_compress') From 7d6b3743808e0841cc8ac7d50e0c5265865496b1 Mon Sep 17 00:00:00 2001 From: Ashhar Hasan Date: Sat, 19 Dec 2015 02:15:14 +0530 Subject: [PATCH 4/7] Separate challenge for better compression --- arrays_strings/better_compress/__init__.py | 0 .../better_compress_challenge.ipynb | 165 +++++++++++ .../better_compress_solution.ipynb | 263 ++++++++++++++++++ .../better_compress/test_compress.py | 21 ++ 4 files changed, 449 insertions(+) create mode 100644 arrays_strings/better_compress/__init__.py create mode 100644 arrays_strings/better_compress/better_compress_challenge.ipynb create mode 100644 arrays_strings/better_compress/better_compress_solution.ipynb create mode 100644 arrays_strings/better_compress/test_compress.py diff --git a/arrays_strings/better_compress/__init__.py b/arrays_strings/better_compress/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arrays_strings/better_compress/better_compress_challenge.ipynb b/arrays_strings/better_compress/better_compress_challenge.ipynb new file mode 100644 index 0000000..b1f3afe --- /dev/null +++ b/arrays_strings/better_compress/better_compress_challenge.ipynb @@ -0,0 +1,165 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3BCCD4'. Only compress the string if it saves space.\n", + "\n", + "* [Constraints](#Constraints)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)\n", + "* [Solution Notebook](#Solution-Notebook)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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", + "* Can you use additional data structures? \n", + " * Yes\n", + "* Is this case sensitive?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* None -> None\n", + "* '' -> ''\n", + "* 'AABBCC' -> 'AABBCC'\n", + "* 'AAABCCDDDD' -> 'A3BCCD4'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/compress/compress_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def compress_string(string):\n", + " # TODO: Implement me\n", + " pass" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "**The following unit test is expected to fail until you solve the challenge.**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# %load test_compress.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestCompress(object):\n", + "\n", + " def test_compress(self, func):\n", + " assert_equal(func(None), None)\n", + " assert_equal(func(''), '')\n", + " assert_equal(func('AABBCC'), 'AABBCC')\n", + " assert_equal(func('AAABCCDDDD'), 'A3BCCD4')\n", + " print('Success: test_compress')\n", + "\n", + "\n", + "def main():\n", + " test = TestCompress()\n", + " test.test_compress(compress_string)\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/compress/compress_solution.ipynb) for a discussion on algorithms and code solutions." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/arrays_strings/better_compress/better_compress_solution.ipynb b/arrays_strings/better_compress/better_compress_solution.ipynb new file mode 100644 index 0000000..3770c03 --- /dev/null +++ b/arrays_strings/better_compress/better_compress_solution.ipynb @@ -0,0 +1,263 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Solution Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3BCCD4'. Only compress the string if it saves space.\n", + "\n", + "* [Constraints](#Constraints)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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", + "* Can you use additional data structures? \n", + " * Yes\n", + "* Is this case sensitive?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* None -> None\n", + "* '' -> ''\n", + "* 'AABBCC' -> 'AABBCC'\n", + "* 'AAABCCDDDD' -> 'A3BCCD4'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 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", + "* Calculate the size of the compressed string\n", + " * Note the constraint about compressing only if it saves space\n", + "* If the compressed string size is >= string size, return string\n", + "* Create compressed_string\n", + " * For each char in string\n", + " * If char is the same as last_char, increment count\n", + " * Else\n", + " * If the count is more than 2\n", + " * Append last_char to compressed_string\n", + " * append count to compressed_string\n", + " * count = 1\n", + " * last_char = char\n", + " * If count is 1\n", + " * Append last_char to compressed_string\n", + " * count = 1\n", + " * last_char = char\n", + " * If count is 2\n", + " * Append last_char to compressed_string\n", + " * Append last_char to compressed_string once more\n", + " * count = 1\n", + " * last_char = char\n", + " * Append last_char to compressed_string\n", + " * Append count to compressed_string\n", + " * Return compressed_string\n", + "\n", + "Complexity:\n", + "* Time: O(n)\n", + "* Space: O(n)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def compress_string(string):\n", + "\tif string is None or len(string) == 0:\n", + "\t\treturn string\n", + "\n", + "\t# Calculate the size of the compressed string\n", + "\tsize = 0\n", + "\tlast_char = string[0]\n", + "\tfor char in string:\n", + "\t\tif char != last_char:\n", + "\t\t\tsize += 2\n", + "\t\t\tlast_char = char\n", + "\tsize += 2\n", + "\n", + "\t# If the compressed string size is greater than\n", + "\t# or equal to string size, return original string\n", + "\tif size >= len(string):\n", + "\t\treturn string\n", + "\n", + "\t# Create compressed_string\n", + "\t# New objective:\n", + "\t\t# Single characters are to be left as is\n", + "\t\t# Double characters are to be left as are\n", + "\tcompressed_string = list()\n", + "\tcount = 0\n", + "\tlast_char = string[0]\n", + "\tfor char in string:\n", + "\t\tif char == last_char:\n", + "\t\t\tcount += 1\n", + "\t\telse:\n", + "\t\t\t# Do the old compression tricks only if count exceeds two\n", + "\t\t\tif count > 2:\n", + "\t\t\t\tcompressed_string.append(last_char)\n", + "\t\t\t\tcompressed_string.append(str(count))\n", + "\t\t\t\tcount = 1\n", + "\t\t\t\tlast_char = char\n", + "\t\t\t# If count is either 1 or 2 (or 0?)\n", + "\t\t\t# FIXME: CAN COUNT BE 0?\n", + "\t\t\telse:\n", + "\t\t\t\t# If count is 1, leave the char as is\n", + "\t\t\t\tif count == 1:\n", + "\t\t\t\t\tcompressed_string.append(last_char)\n", + "\t\t\t\t\tcount = 1\n", + "\t\t\t\t\tlast_char = char\n", + "\t\t\t\t# If count is 2, append the character twice\n", + "\t\t\t\telse:\n", + "\t\t\t\t\tcompressed_string.append(last_char)\n", + "\t\t\t\t\tcompressed_string.append(last_char)\n", + "\t\t\t\t\tcount = 1\n", + "\t\t\t\t\tlast_char = char\n", + "\tcompressed_string.append(last_char)\n", + "\tcompressed_string.append(str(count))\n", + "\n", + "\t# Convert the characters in the list to a string\n", + "\treturn \"\".join(compressed_string)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting test_compress.py\n" + ] + } + ], + "source": [ + "%%writefile test_compress.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestCompress(object):\n", + "\n", + " def test_compress(self, func):\n", + " assert_equal(func(None), None)\n", + " assert_equal(func(''), '')\n", + " assert_equal(func('AABBCC'), 'AABBCC')\n", + " assert_equal(func('AAABCCDDDD'), 'A3BCCD4')\n", + " assert_equal(func('aaBCCEFFFFKKMMMMMMP taaammanlaarrrr seeeeeeeeek tooo'), 'aaBCCEF4KKM6P ta3mmanlaar4 se9k to3')\n", + " print('Success: test_compress')\n", + "\n", + "\n", + "def main():\n", + " test = TestCompress()\n", + " test.test_compress(compress_string)\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_compress\n" + ] + } + ], + "source": [ + "%run -i test_compress.py" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/arrays_strings/better_compress/test_compress.py b/arrays_strings/better_compress/test_compress.py new file mode 100644 index 0000000..b15dde4 --- /dev/null +++ b/arrays_strings/better_compress/test_compress.py @@ -0,0 +1,21 @@ +from nose.tools import assert_equal + + +class TestCompress(object): + + def test_compress(self, func): + assert_equal(func(None), None) + assert_equal(func(''), '') + assert_equal(func('AABBCC'), 'AABBCC') + assert_equal(func('AAABCCDDDD'), 'A3BCCD4') + assert_equal(func('aaBCCEFFFFKKMMMMMMP taaammanlaarrrr seeeeeeeeek tooo'), 'aaBCCEF4KKM6P ta3mmanlaar4 se9k to3') + print('Success: test_compress') + + +def main(): + test = TestCompress() + test.test_compress(compress_string) + + +if __name__ == '__main__': + main() \ No newline at end of file From bbb9b74f0f74a89e9d6de2fe4400f1d8f9413fca Mon Sep 17 00:00:00 2001 From: Ashhar Hasan Date: Sat, 19 Dec 2015 02:20:59 +0530 Subject: [PATCH 5/7] Restored original compression challenge --- .../compress/compress_challenge.ipynb | 16 +-- .../compress/compress_solution.ipynb | 129 ++++++------------ arrays_strings/compress/test_compress.py | 3 +- 3 files changed, 54 insertions(+), 94 deletions(-) diff --git a/arrays_strings/compress/compress_challenge.ipynb b/arrays_strings/compress/compress_challenge.ipynb index b1f3afe..fb17544 100644 --- a/arrays_strings/compress/compress_challenge.ipynb +++ b/arrays_strings/compress/compress_challenge.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3BCCD4'. Only compress the string if it saves space.\n", + "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3B1C2D4'. Only compress the string if it saves space.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", @@ -52,7 +52,7 @@ "* None -> None\n", "* '' -> ''\n", "* 'AABBCC' -> 'AABBCC'\n", - "* 'AAABCCDDDD' -> 'A3BCCD4'" + "* 'AAABCCDDDD' -> 'A3B1C2D4'" ] }, { @@ -118,7 +118,7 @@ " assert_equal(func(None), None)\n", " assert_equal(func(''), '')\n", " assert_equal(func('AABBCC'), 'AABBCC')\n", - " assert_equal(func('AAABCCDDDD'), 'A3BCCD4')\n", + " assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')\n", " print('Success: test_compress')\n", "\n", "\n", @@ -143,21 +143,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 2", "language": "python", - "name": "python3" + "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 3 + "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.4.3" + "pygments_lexer": "ipython2", + "version": "2.7.10" } }, "nbformat": 4, diff --git a/arrays_strings/compress/compress_solution.ipynb b/arrays_strings/compress/compress_solution.ipynb index 3770c03..aa11798 100644 --- a/arrays_strings/compress/compress_solution.ipynb +++ b/arrays_strings/compress/compress_solution.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3BCCD4'. Only compress the string if it saves space.\n", + "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3B1C2D4'. Only compress the string if it saves space.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", @@ -51,7 +51,7 @@ "* None -> None\n", "* '' -> ''\n", "* 'AABBCC' -> 'AABBCC'\n", - "* 'AAABCCDDDD' -> 'A3BCCD4'" + "* 'AAABCCDDDD' -> 'A3B1C2D4'" ] }, { @@ -69,20 +69,10 @@ " * For each char in string\n", " * If char is the same as last_char, increment count\n", " * Else\n", - " * If the count is more than 2\n", - " * Append last_char to compressed_string\n", - " * append count to compressed_string\n", - " * count = 1\n", - " * last_char = char\n", - " * If count is 1\n", - " * Append last_char to compressed_string\n", - " * count = 1\n", - " * last_char = char\n", - " * If count is 2\n", - " * Append last_char to compressed_string\n", - " * Append last_char to compressed_string once more\n", - " * count = 1\n", - " * last_char = char\n", + " * Append last_char to compressed_string\n", + " * append count to compressed_string\n", + " * count = 1\n", + " * last_char = char\n", " * Append last_char to compressed_string\n", " * Append count to compressed_string\n", " * Return compressed_string\n", @@ -101,66 +91,47 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def compress_string(string):\n", - "\tif string is None or len(string) == 0:\n", - "\t\treturn string\n", + " if string is None or len(string) == 0:\n", + " return string\n", "\n", - "\t# Calculate the size of the compressed string\n", - "\tsize = 0\n", - "\tlast_char = string[0]\n", - "\tfor char in string:\n", - "\t\tif char != last_char:\n", - "\t\t\tsize += 2\n", - "\t\t\tlast_char = char\n", - "\tsize += 2\n", + " # Calculate the size of the compressed string\n", + " size = 0\n", + " last_char = string[0]\n", + " for char in string:\n", + " if char != last_char:\n", + " size += 2\n", + " last_char = char\n", + " size += 2\n", "\n", - "\t# If the compressed string size is greater than\n", - "\t# or equal to string size, return original string\n", - "\tif size >= len(string):\n", - "\t\treturn string\n", + " # If the compressed string size is greater than\n", + " # or equal to string size, return original string\n", + " if size >= len(string):\n", + " return string\n", "\n", - "\t# Create compressed_string\n", - "\t# New objective:\n", - "\t\t# Single characters are to be left as is\n", - "\t\t# Double characters are to be left as are\n", - "\tcompressed_string = list()\n", - "\tcount = 0\n", - "\tlast_char = string[0]\n", - "\tfor char in string:\n", - "\t\tif char == last_char:\n", - "\t\t\tcount += 1\n", - "\t\telse:\n", - "\t\t\t# Do the old compression tricks only if count exceeds two\n", - "\t\t\tif count > 2:\n", - "\t\t\t\tcompressed_string.append(last_char)\n", - "\t\t\t\tcompressed_string.append(str(count))\n", - "\t\t\t\tcount = 1\n", - "\t\t\t\tlast_char = char\n", - "\t\t\t# If count is either 1 or 2 (or 0?)\n", - "\t\t\t# FIXME: CAN COUNT BE 0?\n", - "\t\t\telse:\n", - "\t\t\t\t# If count is 1, leave the char as is\n", - "\t\t\t\tif count == 1:\n", - "\t\t\t\t\tcompressed_string.append(last_char)\n", - "\t\t\t\t\tcount = 1\n", - "\t\t\t\t\tlast_char = char\n", - "\t\t\t\t# If count is 2, append the character twice\n", - "\t\t\t\telse:\n", - "\t\t\t\t\tcompressed_string.append(last_char)\n", - "\t\t\t\t\tcompressed_string.append(last_char)\n", - "\t\t\t\t\tcount = 1\n", - "\t\t\t\t\tlast_char = char\n", - "\tcompressed_string.append(last_char)\n", - "\tcompressed_string.append(str(count))\n", + " # Create compressed_string\n", + " compressed_string = list()\n", + " count = 0\n", + " last_char = string[0]\n", + " for char in string:\n", + " if char == last_char:\n", + " count += 1\n", + " else:\n", + " compressed_string.append(last_char)\n", + " compressed_string.append(str(count))\n", + " count = 1\n", + " last_char = char\n", + " compressed_string.append(last_char)\n", + " compressed_string.append(str(count))\n", "\n", - "\t# Convert the characters in the list to a string\n", - "\treturn \"\".join(compressed_string)" + " # Convert the characters in the list to a string\n", + " return \"\".join(compressed_string)" ] }, { @@ -172,7 +143,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -196,8 +167,7 @@ " assert_equal(func(None), None)\n", " assert_equal(func(''), '')\n", " assert_equal(func('AABBCC'), 'AABBCC')\n", - " assert_equal(func('AAABCCDDDD'), 'A3BCCD4')\n", - " assert_equal(func('aaBCCEFFFFKKMMMMMMP taaammanlaarrrr seeeeeeeeek tooo'), 'aaBCCEF4KKM6P ta3mmanlaar4 se9k to3')\n", + " assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')\n", " print('Success: test_compress')\n", "\n", "\n", @@ -212,7 +182,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -228,34 +198,25 @@ "source": [ "%run -i test_compress.py" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 2", "language": "python", - "name": "python3" + "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 3 + "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.4.3" + "pygments_lexer": "ipython2", + "version": "2.7.10" } }, "nbformat": 4, diff --git a/arrays_strings/compress/test_compress.py b/arrays_strings/compress/test_compress.py index b15dde4..7ce5142 100644 --- a/arrays_strings/compress/test_compress.py +++ b/arrays_strings/compress/test_compress.py @@ -7,8 +7,7 @@ class TestCompress(object): assert_equal(func(None), None) assert_equal(func(''), '') assert_equal(func('AABBCC'), 'AABBCC') - assert_equal(func('AAABCCDDDD'), 'A3BCCD4') - assert_equal(func('aaBCCEFFFFKKMMMMMMP taaammanlaarrrr seeeeeeeeek tooo'), 'aaBCCEF4KKM6P ta3mmanlaar4 se9k to3') + assert_equal(func('AAABCCDDDD'), 'A3B1C2D4') print('Success: test_compress') From ec6ced98a1a35493d1bccd5e00f71ca87f299be5 Mon Sep 17 00:00:00 2001 From: Ashhar Hasan Date: Sat, 19 Dec 2015 02:32:46 +0530 Subject: [PATCH 6/7] Replaced tabs with spaces --- .../better_compress_solution.ipynb | 100 +++++++++--------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/arrays_strings/better_compress/better_compress_solution.ipynb b/arrays_strings/better_compress/better_compress_solution.ipynb index 3770c03..89acb96 100644 --- a/arrays_strings/better_compress/better_compress_solution.ipynb +++ b/arrays_strings/better_compress/better_compress_solution.ipynb @@ -108,59 +108,59 @@ "outputs": [], "source": [ "def compress_string(string):\n", - "\tif string is None or len(string) == 0:\n", - "\t\treturn string\n", + " if string is None or len(string) == 0:\n", + " return string\n", "\n", - "\t# Calculate the size of the compressed string\n", - "\tsize = 0\n", - "\tlast_char = string[0]\n", - "\tfor char in string:\n", - "\t\tif char != last_char:\n", - "\t\t\tsize += 2\n", - "\t\t\tlast_char = char\n", - "\tsize += 2\n", + " # Calculate the size of the compressed string\n", + " size = 0\n", + " last_char = string[0]\n", + " for char in string:\n", + " if char != last_char:\n", + " size += 2\n", + " last_char = char\n", + " size += 2\n", "\n", - "\t# If the compressed string size is greater than\n", - "\t# or equal to string size, return original string\n", - "\tif size >= len(string):\n", - "\t\treturn string\n", + " # If the compressed string size is greater than\n", + " # or equal to string size, return original string\n", + " if size >= len(string):\n", + " return string\n", "\n", - "\t# Create compressed_string\n", - "\t# New objective:\n", - "\t\t# Single characters are to be left as is\n", - "\t\t# Double characters are to be left as are\n", - "\tcompressed_string = list()\n", - "\tcount = 0\n", - "\tlast_char = string[0]\n", - "\tfor char in string:\n", - "\t\tif char == last_char:\n", - "\t\t\tcount += 1\n", - "\t\telse:\n", - "\t\t\t# Do the old compression tricks only if count exceeds two\n", - "\t\t\tif count > 2:\n", - "\t\t\t\tcompressed_string.append(last_char)\n", - "\t\t\t\tcompressed_string.append(str(count))\n", - "\t\t\t\tcount = 1\n", - "\t\t\t\tlast_char = char\n", - "\t\t\t# If count is either 1 or 2 (or 0?)\n", - "\t\t\t# FIXME: CAN COUNT BE 0?\n", - "\t\t\telse:\n", - "\t\t\t\t# If count is 1, leave the char as is\n", - "\t\t\t\tif count == 1:\n", - "\t\t\t\t\tcompressed_string.append(last_char)\n", - "\t\t\t\t\tcount = 1\n", - "\t\t\t\t\tlast_char = char\n", - "\t\t\t\t# If count is 2, append the character twice\n", - "\t\t\t\telse:\n", - "\t\t\t\t\tcompressed_string.append(last_char)\n", - "\t\t\t\t\tcompressed_string.append(last_char)\n", - "\t\t\t\t\tcount = 1\n", - "\t\t\t\t\tlast_char = char\n", - "\tcompressed_string.append(last_char)\n", - "\tcompressed_string.append(str(count))\n", + " # Create compressed_string\n", + " # New objective:\n", + " # Single characters are to be left as is\n", + " # Double characters are to be left as are\n", + " compressed_string = list()\n", + " count = 0\n", + " last_char = string[0]\n", + " for char in string:\n", + " if char == last_char:\n", + " count += 1\n", + " else:\n", + " # Do the old compression tricks only if count exceeds two\n", + " if count > 2:\n", + " compressed_string.append(last_char)\n", + " compressed_string.append(str(count))\n", + " count = 1\n", + " last_char = char\n", + " # If count is either 1 or 2 (or 0?)\n", + " # FIXME: CAN COUNT BE 0?\n", + " else:\n", + " # If count is 1, leave the char as is\n", + " if count == 1:\n", + " compressed_string.append(last_char)\n", + " count = 1\n", + " last_char = char\n", + " # If count is 2, append the character twice\n", + " else:\n", + " compressed_string.append(last_char)\n", + " compressed_string.append(last_char)\n", + " count = 1\n", + " last_char = char\n", + " compressed_string.append(last_char)\n", + " compressed_string.append(str(count))\n", "\n", - "\t# Convert the characters in the list to a string\n", - "\treturn \"\".join(compressed_string)" + " # Convert the characters in the list to a string\n", + " return \"\".join(compressed_string)" ] }, { @@ -255,7 +255,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.5.0" } }, "nbformat": 4, From aa24c9a1731a42ac13921a0b24755b8e3cc2aad3 Mon Sep 17 00:00:00 2001 From: Ashhar Hasan Date: Sat, 19 Dec 2015 02:36:11 +0530 Subject: [PATCH 7/7] Removed the fixme --- arrays_strings/better_compress/better_compress_solution.ipynb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arrays_strings/better_compress/better_compress_solution.ipynb b/arrays_strings/better_compress/better_compress_solution.ipynb index 89acb96..2e9a0c0 100644 --- a/arrays_strings/better_compress/better_compress_solution.ipynb +++ b/arrays_strings/better_compress/better_compress_solution.ipynb @@ -142,8 +142,7 @@ " compressed_string.append(str(count))\n", " count = 1\n", " last_char = char\n", - " # If count is either 1 or 2 (or 0?)\n", - " # FIXME: CAN COUNT BE 0?\n", + " # If count is either 1 or 2\n", " else:\n", " # If count is 1, leave the char as is\n", " if count == 1:\n",