mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Updated unit test to use nose and moved it to Test Cases section. Tweaked algorithm description.
This commit is contained in:
parent
83e3eedb52
commit
4d1b78617a
|
@ -38,21 +38,51 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* NULL\n",
|
"* NULL->NULL\n",
|
||||||
"* ' ' -> '%20'\n",
|
"* ' ' -> '%20'\n",
|
||||||
"* ' foo bar ' -> '%20foo%20bar%20'\n",
|
"* ' foo bar ' -> '%20foo%20bar%20'\n",
|
||||||
"* 'foo' -> 'foo'"
|
"* 'foo' -> 'foo'"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from nose.tools import assert_equal\n",
|
||||||
|
"\n",
|
||||||
|
"class Test(object):\n",
|
||||||
|
" def test_replace_char(self, func):\n",
|
||||||
|
" str0 = None\n",
|
||||||
|
" str1 = bytearray(' ||')\n",
|
||||||
|
" str2 = bytearray(' foo bar ||||||')\n",
|
||||||
|
" str3 = bytearray('foo')\n",
|
||||||
|
" encode_spaces(str0, 0)\n",
|
||||||
|
" encode_spaces(str1, 1)\n",
|
||||||
|
" encode_spaces(str2, 9)\n",
|
||||||
|
" encode_spaces(str3, 3)\n",
|
||||||
|
" assert_equal(str0, None)\n",
|
||||||
|
" assert_equal(str1, '%20')\n",
|
||||||
|
" assert_equal(str2, '%20foo%20bar%20')\n",
|
||||||
|
" assert_equal(str3, 'foo')\n",
|
||||||
|
"\n",
|
||||||
|
"def run_tests(func):\n",
|
||||||
|
" test = Test()\n",
|
||||||
|
" test.test_replace_char(func)"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Algorithm\n",
|
"## Algorithm\n",
|
||||||
"\n",
|
"\n",
|
||||||
"![alt text](https://raw.githubusercontent.com/donnemartin/algorithms-data-structures/master/images/replace_string.jpg)\n",
|
"Since Python strings are immutable, we'll use a bytearray 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). For the python bytearray we will not use a null terminator.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Since Python strings are immutable, we'll use a bytearray instead to exercise in-place string manipulation as you would get with a C string (which is null terminated, as seen in the above diagram). For the python bytearray we will not use a null terminator.\n",
|
"![alt text](https://raw.githubusercontent.com/donnemartin/algorithms-data-structures/master/images/replace_string.jpg)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Count the number of spaces in the bytearray\n",
|
"* Count the number of spaces in the bytearray\n",
|
||||||
"* Determine the new bytearray length\n",
|
"* Determine the new bytearray length\n",
|
||||||
|
@ -101,18 +131,7 @@
|
||||||
" string[new_length] = string[i]\n",
|
" string[new_length] = string[i]\n",
|
||||||
" new_length -= 1\n",
|
" new_length -= 1\n",
|
||||||
"\n",
|
"\n",
|
||||||
"str0 = None\n",
|
"run_tests(encode_spaces)"
|
||||||
"str1 = bytearray(' ||')\n",
|
|
||||||
"str2 = bytearray(' foo bar ||||||')\n",
|
|
||||||
"str3 = bytearray('foo')\n",
|
|
||||||
"encode_spaces(str0, 0)\n",
|
|
||||||
"encode_spaces(str1, 1)\n",
|
|
||||||
"encode_spaces(str2, 9)\n",
|
|
||||||
"encode_spaces(str3, 3)\n",
|
|
||||||
"print(str0)\n",
|
|
||||||
"print(str1)\n",
|
|
||||||
"print(str2)\n",
|
|
||||||
"print(str3)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -138,7 +157,10 @@
|
||||||
" return re.sub(' ', '%20', string)\n",
|
" return re.sub(' ', '%20', string)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def encode_spaces_alt2(string):\n",
|
"def encode_spaces_alt2(string):\n",
|
||||||
" return string.replace(' ', '%20')"
|
" return string.replace(' ', '%20')\n",
|
||||||
|
"\n",
|
||||||
|
"run_tests(encode_spaces_alt)\n",
|
||||||
|
"run_tests(encode_spaces_alt2)"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in New Issue
Block a user