mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-12 20:38:02 +00:00
Added compress challenge.
This commit is contained in:
@@ -4,7 +4,14 @@
|
||||
"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://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": [
|
||||
"## Constraints\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",
|
||||
"* Can I assume the string is ASCII?\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",
|
||||
@@ -47,9 +54,9 @@
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* NULL\n",
|
||||
"* None -> None\n",
|
||||
"* '' -> ''\n",
|
||||
"* 'ABC' -> 'ABC'\n",
|
||||
"* 'AABBCC' -> 'AABBCC'\n",
|
||||
"* 'AAABCCDDDD' -> 'A3B1C2D4'"
|
||||
]
|
||||
},
|
||||
@@ -59,9 +66,7 @@
|
||||
"source": [
|
||||
"## Algorithm: List\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",
|
||||
"\n",
|
||||
"\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",
|
||||
"* Calculate the size of the compressed string\n",
|
||||
"* If the compressed string size is >= string size, return string\n",
|
||||
@@ -79,7 +84,7 @@
|
||||
"\n",
|
||||
"Complexity:\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": [
|
||||
"## Algorithm: 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 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",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n)\n",
|
||||
"* Space: O(m) where m is the size of the compressed bytearray"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code: Byte Array"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
@@ -201,14 +201,14 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Unit Test"
|
||||
"## Code: Byte Array"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"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": {
|
||||
"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": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -228,20 +271,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\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)"
|
||||
"%run -i test_compress.py"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user