interactive-coding-challenges/arrays-strings/reverse_string.ipynb

163 lines
3.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 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: Not In-Place](#Pythonic-Code:-Not-In-Place)"
]
},
{
"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, do the operation in-place"
]
},
{
"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-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": [
"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
}