Remove specified characters challenge:New algorithm to replace the first simple algorithm. Taking care the PEP8 style

This commit is contained in:
eamanu 2017-04-09 19:36:04 -03:00
parent 379474ccf9
commit fb8b23ced5
3 changed files with 41 additions and 34 deletions

View File

@ -108,10 +108,9 @@
"outputs": [],
"source": [
"class RemoveChars(object):\n",
"\n",
" def remove_chars(self, string, chars):\n",
" # TODO: Implement me\n",
" pass"
" pass\n"
]
},
{
@ -147,8 +146,8 @@
"# %load test_remove_chars.py\n",
"from nose.tools import assert_equal\n",
"\n",
"class TestRemoveChars(object):\n",
"\n",
"class TestRemoveChars(object):\n",
" def test_remove_chars(self, string, func):\n",
" assert_equal(func(string, None), False)\n",
" assert_equal(func(string, ''), 'Python is great')\n",
@ -162,10 +161,8 @@
" remove_chars = RemoveChars()\n",
" string = 'Python is great'\n",
" test.test_remove_chars(string, remove_chars.remove_chars)\n",
" \n",
"\n",
"if __name__ == '__main__':\n",
" main()"
" main()\n"
]
},
{

View File

@ -81,18 +81,18 @@
"editable": true
},
"source": [
"## Algorithm 1: Simple replace of chars\n",
"## Algorithm 1: Simple remove of characters\n",
"\n",
"We just replace the characters for \"\"\n",
"\n",
"* If the chars to remove is None\n",
"* If the characters to remove is None\n",
" * Return False\n",
"* If the chars is empty -> '' \n",
" * Return the same string\n",
"* If is just one characters to delete \n",
" * We replace the characters to \"\" and Return the new string\n",
"* If there are two or more characters to delete\n",
" * We replace the first target character for \"\" from the string, and then replace the second, and then the third... etc\n",
" \n",
"We convert the string (target) and characters (to remove) to a list of characters, 'sl' and 'cl', respectively. Then we make a buffer copy of the string (we called newString). Then: \n",
"\n",
"* Loop the 'sl' and for each 'cl' element, we ask\n",
" * If this sl element == cl element -> remove the sl element in newString\n",
" * Break to avoid repeat process\n",
" \n",
"* Convert the newString list to a string \n",
" "
]
},
@ -108,7 +108,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 1,
"metadata": {
"collapsed": false,
"deletable": true,
@ -117,18 +117,19 @@
"outputs": [],
"source": [
"class RemoveChars(object):\n",
" \n",
" def remove_chars(self, string, chars):\n",
" if chars is None:\n",
" return False\n",
" \n",
" if len(chars) <= 1:\n",
" return string.replace(chars, \"\")\n",
" \n",
" if len(chars) > 1:\n",
" for c in chars:\n",
" string = string.replace(c, \"\")\n",
" return string"
" sl = list(string) # string list\n",
" cl = list(chars) # chars list\n",
" newString = list(string) # string to return\n",
" a = 0\n",
" for i in range(len(sl)):\n",
" for j in range(len(cl)):\n",
" if(sl[i] == cl[j]):\n",
" newString.remove(sl[i])\n",
" break\n",
" return ''.join(newString)\n"
]
},
{
@ -143,7 +144,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 2,
"metadata": {
"collapsed": false,
"deletable": true,
@ -162,8 +163,8 @@
"# %load test_remove_chars.py\n",
"from nose.tools import assert_equal\n",
"\n",
"class TestRemoveChars(object):\n",
"\n",
"class TestRemoveChars(object):\n",
" def test_remove_chars(self, string, func):\n",
" assert_equal(func(string, None), False)\n",
" assert_equal(func(string, ''), 'Python is great')\n",
@ -177,15 +178,13 @@
" remove_chars = RemoveChars()\n",
" string = 'Python is great'\n",
" test.test_remove_chars(string, remove_chars.remove_chars)\n",
" \n",
"\n",
"if __name__ == '__main__':\n",
" main()"
" main()\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 3,
"metadata": {
"collapsed": false,
"deletable": true,
@ -204,6 +203,17 @@
"source": [
"%run -i test_remove_chars.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": []
}
],
"metadata": {

View File

@ -1,7 +1,7 @@
# %load test_remove_chars.py
from nose.tools import assert_equal
class TestRemoveChars(object):
def test_remove_chars(self, string, func):
@ -17,7 +17,7 @@ def main():
remove_chars = RemoveChars()
string = 'Python is great'
test.test_remove_chars(string, remove_chars.remove_chars)
if __name__ == '__main__':
main()