mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Added python solutions to encoding spaces.
This commit is contained in:
parent
24cd55b9aa
commit
9ec2b6e28b
|
@ -4,12 +4,13 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Problem: Given a String, Replace In-Place all Spaces with '%20'\n",
|
"## Problem: Given a string, replace in-place all spaces with '%20'\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
"* [Algorithm](#Algorithm)\n",
|
"* [Algorithm](#Algorithm)\n",
|
||||||
"* [Code](#Code)"
|
"* [Code](#Code)\n",
|
||||||
|
"* [Pythonic-Code: Not In-Place](#Pythonic-Code:-Not-In-Place)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -106,6 +107,32 @@
|
||||||
"print(str2)\n",
|
"print(str2)\n",
|
||||||
"print(str3)"
|
"print(str3)"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Pythonic-Code: Not In-Place\n",
|
||||||
|
"\n",
|
||||||
|
"The following code is Pythonic, but requires using additional data structures as Python strings are immutable. You could use a bytearray or a list instead of a string to simulate manipulating an array of characters."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import re\n",
|
||||||
|
"\n",
|
||||||
|
"def encode_spaces_alt(string):\n",
|
||||||
|
" return re.sub(' ', '%20', string)\n",
|
||||||
|
"\n",
|
||||||
|
"def encode_spaces_alt2(string):\n",
|
||||||
|
" return string.replace(' ', '%20')"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user