mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Add the Remove specified characters in a string challenge and solution its testcases
This commit is contained in:
parent
4144b6f851
commit
379474ccf9
0
arrays_strings/remove_chars/__init__.py
Normal file
0
arrays_strings/remove_chars/__init__.py
Normal file
205
arrays_strings/remove_chars/remove_chars_challenge.ipynb
Normal file
205
arrays_strings/remove_chars/remove_chars_challenge.ipynb
Normal file
@ -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
|
||||
}
|
230
arrays_strings/remove_chars/remove_chars_solution.ipynb
Normal file
230
arrays_strings/remove_chars/remove_chars_solution.ipynb
Normal file
@ -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
|
||||
}
|
23
arrays_strings/remove_chars/test_remove_chars.py
Normal file
23
arrays_strings/remove_chars/test_remove_chars.py
Normal file
@ -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()
|
20
arrays_strings/remove_chars/test_remove_chars.py~
Normal file
20
arrays_strings/remove_chars/test_remove_chars.py~
Normal file
@ -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()
|
Loading…
x
Reference in New Issue
Block a user