mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Added compress challenge.
This commit is contained in:
parent
0c5266a6ad
commit
8b9a1473ea
173
arrays_strings/compress/compress_challenge.ipynb
Normal file
173
arrays_strings/compress/compress_challenge.ipynb
Normal 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
|
||||
}
|
|
@ -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",
|
||||
"![alt text](https://raw.githubusercontent.com/donnemartin/algorithms-data-structures/master/images/compress_string.jpg)\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"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
24
arrays_strings/compress/test_compress.py
Normal file
24
arrays_strings/compress/test_compress.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user