mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
adds "remove specified char in given string" challenge and its solution
This commit is contained in:
parent
6276624914
commit
df107d6278
0
arrays_strings/remove_char/__init__.py
Normal file
0
arrays_strings/remove_char/__init__.py
Normal file
159
arrays_strings/remove_char/remove_char_challenge.ipynb
Normal file
159
arrays_strings/remove_char/remove_char_challenge.ipynb
Normal file
@ -0,0 +1,159 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<small><i>This notebook was prepared by [Mustafa Atik](https://www.linkedin.com/in/muatik). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).</i></small>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Challenge Notebook"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Problem: Implement a funciton which removes specified character in a given string.\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": {},
|
||||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
"* Can I assume the string is ASCII?\n",
|
||||
" * Yes\n",
|
||||
" * Note: Unicode strings could require special handling depending on your language"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* \"a\" from \"tamamdir arAda\" -> \"tmmdir rAd\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Algorithm\n",
|
||||
"\n",
|
||||
"Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/hash_map/hash_map_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": {},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def remove_char(string, char):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Unit Test"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"\n",
|
||||
"\n",
|
||||
"**The following unit test is expected to fail until you solve the challenge.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_hash_map.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestRemoveChar(object):\n",
|
||||
"\n",
|
||||
" # TODO: It would be better if we had unit tests for each\n",
|
||||
" # method in addition to the following end-to-end test\n",
|
||||
" def test_end_to_end(self):\n",
|
||||
" assert_equal(remove_char(\"tamamdir arAda\", \"a\"), \"tmmdir rAd\")\n",
|
||||
" assert_equal(remove_char(\"wlkwlkfew wifiw longgglonggggw\", \"w\"), \"lklkfe ifi longgglongggg\")\n",
|
||||
" assert_equal(remove_char(\"yu*lkke*\", \"*\"), \"yulkke\")\n",
|
||||
" print('Success: remove_car')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestHashMap()\n",
|
||||
" test.test_end_to_end()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Solution Notebook\n",
|
||||
"\n",
|
||||
"Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/hash_map/hash_map_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.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
185
arrays_strings/remove_char/remove_char_solution.ipynb
Normal file
185
arrays_strings/remove_char/remove_char_solution.ipynb
Normal file
@ -0,0 +1,185 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Constraints\n",
|
||||
"\n",
|
||||
"* Can I assume the string is ASCII?\n",
|
||||
" * Yes\n",
|
||||
" * Note: Unicode strings could require special handling depending on your language\n",
|
||||
"\n",
|
||||
"Test Case\n",
|
||||
"\n",
|
||||
"* \"a\" from \"tamamdir arAda\" -> \"tmmdir rAd\"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Algorithm\n",
|
||||
"\n",
|
||||
"Since Python strings are immutable, we'll use a list of chars instead to exercise in-place string manipulation as you would get with a C string.\n",
|
||||
"\n",
|
||||
"create a list as a string builder\n",
|
||||
"\n",
|
||||
"* for each character in string\n",
|
||||
"* * checks whether the current char is not equal to removalChar\n",
|
||||
"* * keep the char in the list\n",
|
||||
" \n",
|
||||
"#### Complexity:\n",
|
||||
"\n",
|
||||
"* Time: O(n)\n",
|
||||
"* Space: O(n)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 45,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def remove_char(string, char):\n",
|
||||
" newString = []\n",
|
||||
" for i in string:\n",
|
||||
" if char != i:\n",
|
||||
" newString.append(i)\n",
|
||||
" return \"\".join(newString)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Pythonic way\n",
|
||||
"\n",
|
||||
"You can achive the same goal in a concise way by using python's string modidication methods as follows:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 46,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def remove_char2(string, char):\n",
|
||||
" return string.translate(None, char)\n",
|
||||
"\n",
|
||||
"def remove_char3(string, char):\n",
|
||||
" return string.replace(char, \"\")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Unittests"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 47,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Success\n",
|
||||
"Success\n",
|
||||
"Success\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def testWith(func):\n",
|
||||
" assert_equal(func(\"tamamdir arAda\", \"a\"), \"tmmdir rAd\")\n",
|
||||
" assert_equal(func(\"wlkwlkfew wifiw longgglonggggw\", \"w\"), \"lklkfe ifi longgglongggg\")\n",
|
||||
" assert_equal(func(\"yu*lkke*\", \"*\"), \"yulkke\")\n",
|
||||
" \n",
|
||||
" print('Success')\n",
|
||||
"testWith(remove_char)\n",
|
||||
"testWith(remove_char2)\n",
|
||||
"testWith(remove_char3)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Benchmark"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 49,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"remove_char 5.82854199409\n",
|
||||
"remove_char2 0.402535915375\n",
|
||||
"remove_char3 0.377779960632\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import timeit\n",
|
||||
"print \"remove_char\", timeit.timeit(\n",
|
||||
" \"remove_char('wlkwlkfew wifiw longgglonggggw', 'a')\",\n",
|
||||
" \"from __main__ import remove_char\", number=1200000)\n",
|
||||
"print \"remove_char2\", timeit.timeit(\n",
|
||||
" \"remove_char2('wlkwlkfew wifiw longgglonggggw', 'a')\",\n",
|
||||
" \"from __main__ import remove_char2\", number=1200000)\n",
|
||||
"print \"remove_char3\", timeit.timeit(\n",
|
||||
" \"remove_char3('wlkwlkfew wifiw longgglonggggw', 'a')\",\n",
|
||||
" \"from __main__ import remove_char3\", number=1200000)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"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.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user