diff --git a/README.md b/README.md index a3c0a6e..4865f29 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/arrays-strings/reverse_string.cpp b/arrays-strings/reverse_string.cpp new file mode 100644 index 0000000..4a04314 --- /dev/null +++ b/arrays-strings/reverse_string.cpp @@ -0,0 +1,36 @@ +#include + +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; +} diff --git a/arrays-strings/reverse_string.ipynb b/arrays-strings/reverse_string.ipynb new file mode 100644 index 0000000..6019830 --- /dev/null +++ b/arrays-strings/reverse_string.ipynb @@ -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 \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 +}