From 379474ccf95e8570ec68b3d833b74dfe6b222315 Mon Sep 17 00:00:00 2001 From: eamanu Date: Fri, 7 Apr 2017 23:30:50 -0300 Subject: [PATCH] Add the Remove specified characters in a string challenge and solution its testcases --- arrays_strings/remove_chars/__init__.py | 0 .../remove_chars/remove_chars_challenge.ipynb | 205 ++++++++++++++++ .../remove_chars/remove_chars_solution.ipynb | 230 ++++++++++++++++++ .../remove_chars/test_remove_chars.py | 23 ++ .../remove_chars/test_remove_chars.py~ | 20 ++ 5 files changed, 478 insertions(+) create mode 100644 arrays_strings/remove_chars/__init__.py create mode 100644 arrays_strings/remove_chars/remove_chars_challenge.ipynb create mode 100644 arrays_strings/remove_chars/remove_chars_solution.ipynb create mode 100644 arrays_strings/remove_chars/test_remove_chars.py create mode 100644 arrays_strings/remove_chars/test_remove_chars.py~ diff --git a/arrays_strings/remove_chars/__init__.py b/arrays_strings/remove_chars/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arrays_strings/remove_chars/remove_chars_challenge.ipynb b/arrays_strings/remove_chars/remove_chars_challenge.ipynb new file mode 100644 index 0000000..b6c5269 --- /dev/null +++ b/arrays_strings/remove_chars/remove_chars_challenge.ipynb @@ -0,0 +1,205 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "This notebook was prepared by [eamanu](http://github.com/eamanu). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "# Challenge Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "## Problem: Remove specified characters in a string\t\n", + "\n", + "* [Constraints](#Constraints)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)\n", + "* [Solution Notebook](#Solution-Notebook)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "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", + "* Can we assume this is case sensitive?\n", + " * Yes\n", + "* Can we use additional data structures?\n", + " * Yes\n", + "* Can we assume this fits in memory?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "## Test Cases\n", + "\n", + "String = 'Python is great'\n", + "chars -> This is the characters to remove from String\n", + "\n", + "* chars = None -> False\n", + "* chars = '' -> 'Python is great'\n", + "* chars = 'y' -> 'Pthon is great'\n", + "* chars = 'tp' -> 'yhon is grea'" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/remove_chars/remove_chars_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "deletable": true, + "editable": true + }, + "outputs": [], + "source": [ + "class RemoveChars(object):\n", + "\n", + " def remove_chars(self, string, chars):\n", + " # TODO: Implement me\n", + " pass" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "## Unit Test" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "**The following unit test is expected to fail until you solve the challenge.**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "deletable": true, + "editable": true + }, + "outputs": [], + "source": [ + "# %load test_remove_chars.py\n", + "from nose.tools import assert_equal\n", + "\n", + "class TestRemoveChars(object):\n", + "\n", + " def test_remove_chars(self, string, func):\n", + " assert_equal(func(string, None), False)\n", + " assert_equal(func(string, ''), 'Python is great')\n", + " assert_equal(func(string, 'y'), 'Pthon is great')\n", + " assert_equal(func(string, 'tP'), 'yhon is grea')\n", + " print('Success: test_remove_chars')\n", + "\n", + "\n", + "def main():\n", + " test = TestRemoveChars()\n", + " 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()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/remove_chars/remove_chars_solution.ipynb) for a discussion on algorithms and code solutions." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.12" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/arrays_strings/remove_chars/remove_chars_solution.ipynb b/arrays_strings/remove_chars/remove_chars_solution.ipynb new file mode 100644 index 0000000..3a0d3cf --- /dev/null +++ b/arrays_strings/remove_chars/remove_chars_solution.ipynb @@ -0,0 +1,230 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "This notebook was prepared by [eamanu](http://github.com/eamanu). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "# Solution Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "## Problem: Remove specified characters in a string\t\n", + "\n", + "* [Constraints](#Constraints)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm 1: Simple replace of chars](#Algorithm-1:-Simple-replace-of-chars)\n", + "* [Code: Simple replace of chars](#Code:-Simple-replace-of-chars)\n", + "* [Unit Test](#Unit-Test)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "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", + "* Can we assume this is case sensitive?\n", + " * Yes\n", + "* Can we use additional data structures?\n", + " * Yes\n", + "* Can we assume this fits in memory?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "## Test Cases\n", + "\n", + "String = 'Python is great'\n", + "chars -> This is the characters to remove from String\n", + "\n", + "* chars = None -> False\n", + "* chars = '' -> 'Python is great'\n", + "* chars = 'y' -> 'Pthon is great'\n", + "* chars = 'tp' -> 'yhon is grea'" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "## Algorithm 1: Simple replace of chars\n", + "\n", + "We just replace the characters for \"\"\n", + "\n", + "* If the chars 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", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "## Code: Simple replace of chars" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false, + "deletable": true, + "editable": true + }, + "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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true + }, + "source": [ + "## Unit Test" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false, + "deletable": true, + "editable": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_remove_chars\n" + ] + } + ], + "source": [ + "# %load test_remove_chars.py\n", + "from nose.tools import assert_equal\n", + "\n", + "class TestRemoveChars(object):\n", + "\n", + " def test_remove_chars(self, string, func):\n", + " assert_equal(func(string, None), False)\n", + " assert_equal(func(string, ''), 'Python is great')\n", + " assert_equal(func(string, 'y'), 'Pthon is great')\n", + " assert_equal(func(string, 'tP'), 'yhon is grea')\n", + " print('Success: test_remove_chars')\n", + "\n", + "\n", + "def main():\n", + " test = TestRemoveChars()\n", + " 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()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false, + "deletable": true, + "editable": true, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_remove_chars\n" + ] + } + ], + "source": [ + "%run -i test_remove_chars.py" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.12" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/arrays_strings/remove_chars/test_remove_chars.py b/arrays_strings/remove_chars/test_remove_chars.py new file mode 100644 index 0000000..6c22768 --- /dev/null +++ b/arrays_strings/remove_chars/test_remove_chars.py @@ -0,0 +1,23 @@ +# %load test_remove_chars.py +from nose.tools import assert_equal + + +class TestRemoveChars(object): + + def test_remove_chars(self, string, func): + assert_equal(func(string, None), False) + assert_equal(func(string, ''), 'Python is great') + assert_equal(func(string, 'y'), 'Pthon is great') + assert_equal(func(string, 'tP'), 'yhon is grea') + print('Success: test_remove_chars') + + +def main(): + test = TestRemoveChars() + remove_chars = RemoveChars() + string = 'Python is great' + test.test_remove_chars(string, remove_chars.remove_chars) + + +if __name__ == '__main__': + main() diff --git a/arrays_strings/remove_chars/test_remove_chars.py~ b/arrays_strings/remove_chars/test_remove_chars.py~ new file mode 100644 index 0000000..49d438e --- /dev/null +++ b/arrays_strings/remove_chars/test_remove_chars.py~ @@ -0,0 +1,20 @@ +from +class TestRemoveChars(object): + + def test_remove_chars(self, string, func): + assert_equal(func(string, None), False) + assert_equal(func(string, ''), 'Python is great') + assert_equal(func(string, 'y'), 'Pthon is great') + assert_equal(func(string, 'tP'), 'yhon is grea') + print('Success: test_remove_chars') + + +def main(): + test = TestRemoveChars() + remove_chars = RemoveChars() + string = 'Python is great' + test.test_remove_chars(string, remove_chars.remove_chars) + + +if __name__ == '__main__': + main() \ No newline at end of file