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

247 lines
5.9 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Implement a function to reverse a string.\n",
"\n",
"* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Pythonic-Code](#Pythonic-Code)\n",
"* [C Algorithm](C-Algorithm)\n",
"* [C Code]()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clarifying Questions\n",
"\n",
"* Can I use the slice operator or the reversed function?\n",
" * No\n",
"* Since Python string are immutable, can I use a list instead?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* NULL input\n",
"* '' -> ''\n",
"* 'foo bar' -> 'rab oof'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
" def test_reversed(self, func):\n",
" assert_equal(func(None), None)\n",
" assert_equal(func(''), '')\n",
" assert_equal(func('foo bar'), 'rab oof')\n",
"\n",
"def run_tests(func):\n",
" test = Test()\n",
" test.test_reversed(func)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"* Convert the string to a list\n",
"* Iterate len(string)/2 times, starting with i = 0:\n",
" * Swap i and len(string) - 1 - i\n",
" * Sncrement i\n",
"* Convert the list to a string and return it\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: O(n), additional space converting to/from a list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def reverse_string(string):\n",
" if string is None:\n",
" return None\n",
" string_list = list(string)\n",
" string_length = len(string_list)\n",
" for i in xrange(string_length/2):\n",
" string_list[i], string_list[string_length - 1 - i] = \\\n",
" string_list[string_length - 1 - i], string_list[i]\n",
" return ''.join(string_list)\n",
"\n",
"run_tests(reverse_string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pythonic-Code\n",
"\n",
"This question has an artificial constraint that prevented the use of the slice operator and the reversed method. For completeness, the solutions for these are provided below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def reverse_string_alt(string):\n",
" if string is None:\n",
" return None\n",
" return string[::-1]\n",
"\n",
"def reverse_string_alt2(string):\n",
" if string is None:\n",
" return None \n",
" return ''.join(reversed(string))\n",
"\n",
"run_tests(reverse_string_alt)\n",
"run_tests(reverse_string_alt2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## C Algorithm\n",
"\n",
"This is a classic problem in C/C++\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": [
"## C 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",
"}"
]
}
],
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}