mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-08 10:28:03 +00:00
Appended _solution to current notebooks to prepare for challenge and solution notebooks.
This commit is contained in:
274
arrays_strings/reverse_string/reverse_string_solution.ipynb
Normal file
274
arrays_strings/reverse_string/reverse_string_solution.ipynb
Normal file
@@ -0,0 +1,274 @@
|
||||
{
|
||||
"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",
|
||||
"* [Constraints](#Constraints)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
"* [Pythonic-Code](#Pythonic-Code)\n",
|
||||
"* [Unit Test](#Unit-Test)\n",
|
||||
"* [C Algorithm](C-Algorithm)\n",
|
||||
"* [C Code]()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can 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",
|
||||
"* 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": "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": 1,
|
||||
"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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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": 2,
|
||||
"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))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Unit Test"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"*It is important to identify and run through general and edge cases from the [Test Cases](#Test-Cases) section by hand. You generally will not be asked to write a unit test like what is shown below.*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"\n",
|
||||
"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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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
|
||||
}
|
||||
Reference in New Issue
Block a user