2015-05-01 20:13:47 +08:00
{
"cells": [
2015-06-18 04:36:55 +08:00
{
"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>"
]
},
2015-05-01 20:13:47 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-06-24 10:36:16 +08:00
"## Problem: Implement a function to reverse a string.\n",
2015-05-03 05:13:10 +08:00
"\n",
"* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
2015-06-24 10:38:55 +08:00
"* [Pythonic-Code](#Pythonic-Code)\n",
2015-06-25 06:24:40 +08:00
"* [Unit Test](#Unit-Test)\n",
2015-06-24 10:36:16 +08:00
"* [C Algorithm](C-Algorithm)\n",
2015-06-24 10:38:55 +08:00
"* [C Code]()"
2015-05-01 20:13:47 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clarifying Questions\n",
"\n",
2015-06-25 06:24:40 +08:00
"*Problem statements are sometimes intentionally ambiguous. Asking clarifying questions, identifying constraints, and stating assumptions help to ensure you code the intended solution.*\n",
"\n",
"* Can I assume the string is ASCII?\n",
" * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n",
2015-06-24 10:36:16 +08:00
"* 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"
2015-05-01 20:13:47 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
2015-06-25 06:24:40 +08:00
"*Identifying and running through general and edge cases are important. You generally will not be asked to write a unit test like what is shown below.*\n",
"\n",
2015-05-01 20:13:47 +08:00
"* NULL input\n",
2015-05-03 05:13:10 +08:00
"* '' -> ''\n",
"* 'foo bar' -> 'rab oof'"
2015-05-01 20:13:47 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
2015-06-24 10:36:16 +08:00
"* 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",
2015-06-25 06:24:40 +08:00
"execution_count": 1,
2015-06-24 10:36:16 +08:00
"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",
2015-06-25 06:24:40 +08:00
" string_list[i], string_list[string_length-1-i] = \\\n",
" string_list[string_length-1-i], string_list[i]\n",
" return ''.join(string_list)"
2015-06-24 10:36:16 +08:00
]
},
2015-06-24 10:38:55 +08:00
{
"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",
2015-06-25 06:24:40 +08:00
"execution_count": 2,
2015-06-24 10:38:55 +08:00
"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",
2015-06-25 06:24:40 +08:00
" return ''.join(reversed(string))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_reversed\n",
"Success: test_reversed\n",
"Success: test_reversed\n"
]
}
],
"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",
" print('Success: test_reversed')\n",
2015-06-24 10:38:55 +08:00
"\n",
2015-06-25 06:24:40 +08:00
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_reversed(reverse_string)\n",
" test.test_reversed(reverse_string_alt)\n",
" test.test_reversed(reverse_string_alt2)"
2015-06-24 10:38:55 +08:00
]
},
2015-06-24 10:36:16 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## C Algorithm\n",
"\n",
"This is a classic problem in C/C++\n",
"\n",
2015-05-03 05:13:10 +08:00
"We'll want to keep two pointers:\n",
2015-05-01 20:13:47 +08:00
"* 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",
2015-05-03 05:13:10 +08:00
"* Time: O(n)\n",
"* Space: In-place\n",
2015-05-01 20:13:47 +08:00
"\n",
"Note:\n",
"* Instead of using i, you can use str instead, although this might not be as intuitive."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-06-24 10:36:16 +08:00
"## C Code"
2015-05-01 20:13:47 +08:00
]
},
{
"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",
2015-06-18 04:36:55 +08:00
"version": "2.7.10"
2015-05-01 20:13:47 +08:00
}
},
"nbformat": 4,
"nbformat_minor": 0
}