mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Removed bytearray solution, needs some tweaks to make it work with Python 3.
This commit is contained in:
parent
45fa576bc2
commit
73924c279a
|
@ -34,7 +34,6 @@
|
|||
"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",
|
||||
|
@ -127,12 +126,6 @@
|
|||
"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()"
|
||||
|
|
|
@ -22,10 +22,8 @@
|
|||
"\n",
|
||||
"* [Constraints](#Constraints)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm: List](#Algorithm:-List)\n",
|
||||
"* [Code: List](#Code:-List)\n",
|
||||
"* [Algorithm: Byte Array](#Algorithm:-Byte-Array)\n",
|
||||
"* [Code: Byte array](#Code:-Byte-Array)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
"* [Unit Test](#Unit-Test)"
|
||||
]
|
||||
},
|
||||
|
@ -35,7 +33,6 @@
|
|||
"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",
|
||||
|
@ -63,7 +60,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Algorithm: List\n",
|
||||
"## 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",
|
||||
|
@ -91,7 +88,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code: List"
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -139,73 +136,6 @@
|
|||
" 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",
|
||||
"metadata": {},
|
||||
|
@ -215,7 +145,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -245,12 +175,6 @@
|
|||
"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()"
|
||||
|
@ -258,7 +182,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -267,7 +191,6 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Success: test_compress\n",
|
||||
"Success: test_compress\n"
|
||||
]
|
||||
}
|
||||
|
|
|
@ -13,12 +13,6 @@ class TestCompress(object):
|
|||
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