mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Added notebook solving the following: Implement the function void Reverse(char* str).
This commit is contained in:
parent
95591ef96e
commit
51cab5e686
|
@ -6,6 +6,7 @@ Data structures and algorithms practice problems.
|
|||
## Arrays and Strings
|
||||
|
||||
* [Check for unique characters in a string](http://nbviewer.ipython.org/github/donnemartin/practice/blob/master/arrays-strings/unique_chars.ipynb)
|
||||
* [Reverse characters in a string](http://nbviewer.ipython.org/github/donnemartin/practice/blob/master/arrays-strings/reverse_string.ipynb)
|
||||
|
||||
## License
|
||||
|
||||
|
|
36
arrays-strings/reverse_string.cpp
Normal file
36
arrays-strings/reverse_string.cpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void Reverse(char* str) {
|
||||
if (str) {
|
||||
char* i = str; // first letter
|
||||
char* j = str; // last letter
|
||||
|
||||
// find the end of the string
|
||||
while (*j) {
|
||||
j++;
|
||||
}
|
||||
|
||||
// don't point to the null terminator
|
||||
j--;
|
||||
|
||||
char tmp;
|
||||
|
||||
// swap chars to reverse the string
|
||||
while (i < j) {
|
||||
tmp = *i;
|
||||
*i++ = *j;
|
||||
*j-- = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
char test0[] = "";
|
||||
char test1[] = "foo";
|
||||
Reverse(NULL);
|
||||
Reverse(test0);
|
||||
Reverse(test1);
|
||||
printf("%s \n", test0);
|
||||
printf("%s \n", test1);
|
||||
return 0;
|
||||
}
|
154
arrays-strings/reverse_string.ipynb
Normal file
154
arrays-strings/reverse_string.ipynb
Normal file
|
@ -0,0 +1,154 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Problem: Implement the function void Reverse(char* str)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Clarifying Questions\n",
|
||||
"\n",
|
||||
"* Based on the function signature, it seems you have to implement this in C/C++?\n",
|
||||
" * Yes\n",
|
||||
"* Can you use additional data structures?\n",
|
||||
" * No"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* NULL input\n",
|
||||
"* \"\" -> \"\"\n",
|
||||
"* \"foo bar\" -> \"rab oof\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Algorithm\n",
|
||||
"\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",
|
||||
"To get a pointer to the last char, we need to loop through all characters, take note of null terminator.\n",
|
||||
"\n",
|
||||
"* while i < j\n",
|
||||
" * swap i and j\n",
|
||||
"\n",
|
||||
"Complexity:\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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load reverse_string.cpp\n",
|
||||
"#include <stdio.h>\n",
|
||||
"\n",
|
||||
"void Reverse(char* str) {\n",
|
||||
" if (str) {\n",
|
||||
" char* i = str;\t// first letter\n",
|
||||
" char* j = str;\t// last letter\n",
|
||||
" \n",
|
||||
" // find the end of the string\n",
|
||||
" while (*j) {\n",
|
||||
" j++;\n",
|
||||
" }\n",
|
||||
" \n",
|
||||
" // don't point to the null terminator\n",
|
||||
" j--;\n",
|
||||
" \n",
|
||||
" char tmp;\n",
|
||||
" \n",
|
||||
" // swap chars to reverse the string\n",
|
||||
" while (i < j) {\n",
|
||||
" tmp = *i;\n",
|
||||
" *i++ = *j;\n",
|
||||
" *j-- = tmp;\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"int main() {\n",
|
||||
" char test0[] = \"\";\n",
|
||||
" char test1[] = \"foo\";\n",
|
||||
" Reverse(NULL);\n",
|
||||
" Reverse(test0);\n",
|
||||
" Reverse(test1);\n",
|
||||
" printf(\"%s \\n\", test0);\n",
|
||||
" printf(\"%s \\n\", test1);\n",
|
||||
" return 0;\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Pythonic Solution(s)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def reverse_string_alt(string):\n",
|
||||
" return string[::-1]\n",
|
||||
"\n",
|
||||
"def reverse_string_alt2(string):\n",
|
||||
" return \"\".join(reversed(string))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
Loading…
Reference in New Issue
Block a user