mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Added notebook index. Added note about using additional data structures for the Pythonic solution. Minor cleanup.
This commit is contained in:
parent
7826fea184
commit
e22f7b8d20
|
@ -4,7 +4,13 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Problem: Implement the function void Reverse(char* str)"
|
||||
"## Problem: Implement the function void Reverse(char* str)\n",
|
||||
"\n",
|
||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
"* [Pythonic-Code](#Pythonic-Code)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -26,8 +32,8 @@
|
|||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* NULL input\n",
|
||||
"* \"\" -> \"\"\n",
|
||||
"* \"foo bar\" -> \"rab oof\""
|
||||
"* '' -> ''\n",
|
||||
"* 'foo bar' -> 'rab oof'"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -36,7 +42,7 @@
|
|||
"source": [
|
||||
"## Algorithm\n",
|
||||
"\n",
|
||||
"We'll want to keep two pointers\n",
|
||||
"We'll want to keep two pointers:\n",
|
||||
"* i is a pointer to the first char\n",
|
||||
"* j is a pointer to the last char\n",
|
||||
"\n",
|
||||
|
@ -46,8 +52,8 @@
|
|||
" * swap i and j\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n).\n",
|
||||
"* Space: In-place.\n",
|
||||
"* Time: O(n)\n",
|
||||
"* Space: In-place\n",
|
||||
"\n",
|
||||
"Note:\n",
|
||||
"* Instead of using i, you can use str instead, although this might not be as intuitive."
|
||||
|
@ -111,7 +117,9 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Pythonic Solution(s)"
|
||||
"## Pythonic Code\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."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -126,7 +134,7 @@
|
|||
" return string[::-1]\n",
|
||||
"\n",
|
||||
"def reverse_string_alt2(string):\n",
|
||||
" return \"\".join(reversed(string))"
|
||||
" return ''.join(reversed(string))"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
Loading…
Reference in New Issue
Block a user