Removed bytearray solution, needs some tweaks to make it work with Python 3.

This commit is contained in:
Donne Martin 2015-07-06 06:30:00 -04:00
parent 45fa576bc2
commit 73924c279a
3 changed files with 6 additions and 96 deletions

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can we 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",
@ -127,12 +126,6 @@
"def main():\n", "def main():\n",
" test = TestCompress()\n", " test = TestCompress()\n",
" test.test_compress(compress_string)\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", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"

View File

@ -22,10 +22,8 @@
"\n", "\n",
"* [Constraints](#Constraints)\n", "* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n", "* [Test Cases](#Test-Cases)\n",
"* [Algorithm: List](#Algorithm:-List)\n", "* [Algorithm](#Algorithm)\n",
"* [Code: List](#Code:-List)\n", "* [Code](#Code)\n",
"* [Algorithm: Byte Array](#Algorithm:-Byte-Array)\n",
"* [Code: Byte array](#Code:-Byte-Array)\n",
"* [Unit Test](#Unit-Test)" "* [Unit Test](#Unit-Test)"
] ]
}, },
@ -35,7 +33,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can we 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",
@ -63,7 +60,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Algorithm: List\n", "## Algorithm\n",
"\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", "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", "\n",
@ -91,7 +88,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Code: List" "## Code"
] ]
}, },
{ {
@ -139,73 +136,6 @@
" return \"\".join(compressed_string)" " 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", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -215,7 +145,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -245,12 +175,6 @@
"def main():\n", "def main():\n",
" test = TestCompress()\n", " test = TestCompress()\n",
" test.test_compress(compress_string)\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", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
@ -258,7 +182,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 3,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -267,7 +191,6 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Success: test_compress\n",
"Success: test_compress\n" "Success: test_compress\n"
] ]
} }

View File

@ -13,12 +13,6 @@ class TestCompress(object):
def main(): def main():
test = TestCompress() test = TestCompress()
test.test_compress(compress_string) 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__': if __name__ == '__main__':
main() main()