Added compress challenge.

This commit is contained in:
Donne Martin 2015-07-03 19:55:56 -04:00
parent 0c5266a6ad
commit 8b9a1473ea
3 changed files with 260 additions and 33 deletions

View File

@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3B1C2D4'\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",
"*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\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\n",
"* Do you compress even if it doesn't save space?\n",
" * No"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* None -> None\n",
"* '' -> ''\n",
"* 'AABBCC' -> 'AABBCC'\n",
"* 'AAABCCDDDD' -> 'A3B1C2D4'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/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'), 'A3B1C2D4')\n",
" print('Success: test_compress')\n",
"\n",
"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()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/coding-challenges/blob/master/arrays_strings/compress/compress_solution.ipynb) for a discussion on algorithms and code solutions."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@ -4,7 +4,14 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>" "<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution Notebook"
] ]
}, },
{ {
@ -28,9 +35,9 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n", "*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
"\n", "\n",
"* Can I assume the string is ASCII?\n", "* Can we assume the string is ASCII?\n",
" * Yes\n", " * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n", " * Note: Unicode strings could require special handling depending on your language\n",
"* Can you use additional data structures? \n", "* Can you use additional data structures? \n",
@ -47,9 +54,9 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* NULL\n", "* None -> None\n",
"* '' -> ''\n", "* '' -> ''\n",
"* 'ABC' -> 'ABC'\n", "* 'AABBCC' -> 'AABBCC'\n",
"* 'AAABCCDDDD' -> 'A3B1C2D4'" "* 'AAABCCDDDD' -> 'A3B1C2D4'"
] ]
}, },
@ -59,9 +66,7 @@
"source": [ "source": [
"## Algorithm: List\n", "## Algorithm: List\n",
"\n", "\n",
"Since Python strings are immutable, we'll use a list of characters instead to exercise in-place string manipulation as you would get with a C string (which is null terminated, as seen in the diagram below). Python does not use a null-terminator.\n", "Since Python strings are immutable, we'll use a list of characters instead to exercise string manipulation as you would get with a C string. We'll convert the list to a string at the end of the algorithm.\n",
"\n",
"![alt text](https://raw.githubusercontent.com/donnemartin/algorithms-data-structures/master/images/compress_string.jpg)\n",
"\n", "\n",
"* Calculate the size of the compressed string\n", "* Calculate the size of the compressed string\n",
"* If the compressed string size is >= string size, return string\n", "* If the compressed string size is >= string size, return string\n",
@ -79,7 +84,7 @@
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(n)\n", "* Time: O(n)\n",
"* Space: O(2m) where m is the size of the compressed list and the resulting string copied from the list" "* Space: O(n) additional space for the list"
] ]
}, },
{ {
@ -138,20 +143,15 @@
"source": [ "source": [
"## Algorithm: Byte Array\n", "## Algorithm: Byte Array\n",
"\n", "\n",
"The byte array algorithm similar when using a list, except we will need to work with the bytearray's character codes instead of the characters as we did above when we implemented this solution with a list.\n", "As a bonus solution, we can 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 we did above when we implemented this solution with a list.\n",
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(n)\n", "* Time: O(n)\n",
"* Space: O(m) where m is the size of the compressed bytearray" "* Space: O(m) where m is the size of the compressed bytearray"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code: Byte Array"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
@ -201,14 +201,14 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Unit Test" "## Code: Byte Array"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"*It is important to identify and run through general and edge cases from the [Test Cases](#Test-Cases) section by hand. You generally will not be asked to write a unit test like what is shown below.*" "## Unit Test"
] ]
}, },
{ {
@ -217,6 +217,49 @@
"metadata": { "metadata": {
"collapsed": false "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'), 'A3B1C2D4')\n",
" print('Success: test_compress')\n",
"\n",
"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()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -228,20 +271,7 @@
} }
], ],
"source": [ "source": [
"from nose.tools import assert_equal\n", "%run -i test_compress.py"
"\n",
"class Test(object):\n",
" def test_compress(self, func):\n",
" assert_equal(func(None), None)\n",
" assert_equal(func(''), '')\n",
" assert_equal(func('ABC'), 'ABC')\n",
" assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')\n",
" print('Success: test_compress')\n",
"\n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_compress(compress_string)\n",
" test.test_compress(compress_string_alt)"
] ]
} }
], ],

View File

@ -0,0 +1,24 @@
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'), 'A3B1C2D4')
print('Success: test_compress')
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()