From fb8b23ced5b232322f2cfe4d42bb77274e662532 Mon Sep 17 00:00:00 2001 From: eamanu Date: Sun, 9 Apr 2017 19:36:04 -0300 Subject: [PATCH] Remove specified characters challenge:New algorithm to replace the first simple algorithm. Taking care the PEP8 style --- .../remove_chars/remove_chars_challenge.ipynb | 9 +-- .../remove_chars/remove_chars_solution.ipynb | 62 +++++++++++-------- .../remove_chars/test_remove_chars.py | 4 +- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/arrays_strings/remove_chars/remove_chars_challenge.ipynb b/arrays_strings/remove_chars/remove_chars_challenge.ipynb index b6c5269..388530c 100644 --- a/arrays_strings/remove_chars/remove_chars_challenge.ipynb +++ b/arrays_strings/remove_chars/remove_chars_challenge.ipynb @@ -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" ] }, { diff --git a/arrays_strings/remove_chars/remove_chars_solution.ipynb b/arrays_strings/remove_chars/remove_chars_solution.ipynb index 3a0d3cf..693a9bc 100644 --- a/arrays_strings/remove_chars/remove_chars_solution.ipynb +++ b/arrays_strings/remove_chars/remove_chars_solution.ipynb @@ -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": { diff --git a/arrays_strings/remove_chars/test_remove_chars.py b/arrays_strings/remove_chars/test_remove_chars.py index 6c22768..b0242d3 100644 --- a/arrays_strings/remove_chars/test_remove_chars.py +++ b/arrays_strings/remove_chars/test_remove_chars.py @@ -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()