Added python solutions to encoding spaces.

This commit is contained in:
Donne Martin 2015-05-09 11:28:29 -04:00
parent 24cd55b9aa
commit 9ec2b6e28b

View File

@ -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": {