From a567898dab4d00acace87aa7341b29bd7a69b8cf Mon Sep 17 00:00:00 2001 From: mag6367 Date: Thu, 16 Jul 2015 10:20:18 -0500 Subject: [PATCH] new challenge: reverse_words --- arrays_strings/reverse_words/__init__.py | 0 .../reverse_words_challenge.ipynb | 155 +++++++++++++++++ .../reverse_words_solution.ipynb | 162 ++++++++++++++++++ .../reverse_words/reverse_words_solution.py | 16 ++ 4 files changed, 333 insertions(+) create mode 100644 arrays_strings/reverse_words/__init__.py create mode 100644 arrays_strings/reverse_words/reverse_words_challenge.ipynb create mode 100644 arrays_strings/reverse_words/reverse_words_solution.ipynb create mode 100644 arrays_strings/reverse_words/reverse_words_solution.py diff --git a/arrays_strings/reverse_words/__init__.py b/arrays_strings/reverse_words/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arrays_strings/reverse_words/reverse_words_challenge.ipynb b/arrays_strings/reverse_words/reverse_words_challenge.ipynb new file mode 100644 index 0000000..c51736b --- /dev/null +++ b/arrays_strings/reverse_words/reverse_words_challenge.ipynb @@ -0,0 +1,155 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " This notebook was prepared by Marco Guajardo. For license visit [github](https://github.com/donnemartin/interactive-coding-challenges) \n", + "." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge Notebook\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Given a string of words, return a string with the words in reverse" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* [Constraits](#Constraint)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)\n", + "* [Solution Notebook](#Solution-Notebook)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Constraints\n", + "* Can we assume the string is ASCII?\n", + " * Yes\n", + "* Is whitespace important?\n", + " * no the whitespace does not change\n", + "* Is this case sensitive?\n", + " * yes\n", + "* What if the string is empty?\n", + " * return None\n", + "* Is the order of words important?\n", + " * yes\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "* Empty string -> None\n", + "* \"the sun is very hot\" -> \"eht nus si yrev toh\"\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "* Refer to the [Solution](https://github.com/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/reverse_words/reverse_words_solution.ipynb) if you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code " + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def reverse_words (S):\n", + " #TODO: implement me\n", + " pass " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test\n", + " The following unit test is expected to fail until you solve challenge " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from nose.tools import assert_equal\n", + "\n", + "class UnitTest (object):\n", + " def testReverseWords(self):\n", + " assert_equal(func('the sun is hot'), 'eht nus si toh')\n", + " assert_equal(func(''), None)\n", + " assert_equal(func('123 456 789'), '321 654 987')\n", + " assert_equal(func('magic'), 'cigam')\n", + " print('Success: reverse_words')\n", + " \n", + "def main():\n", + " test = UnitTest()\n", + " test.testReverseWords()\n", + "\n", + "if __name__==\"__main__\":\n", + " main()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "* Review the [Solution Notebook](https://github.com/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/reverse_words/reverse_words_solution.ipynb) for discussion on algorithms and code solutions." + ] + } + ], + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/arrays_strings/reverse_words/reverse_words_solution.ipynb b/arrays_strings/reverse_words/reverse_words_solution.ipynb new file mode 100644 index 0000000..e7942a1 --- /dev/null +++ b/arrays_strings/reverse_words/reverse_words_solution.ipynb @@ -0,0 +1,162 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " This notebook was prepared by Marco Guajardo. For license visit [github](https://github.com/donnemartin/interactive-coding-challenges) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Solution notebook\n", + "## Problem: Given a string of words, return a string with the words in reverse" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* [Constraits](#Constraint)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)\n", + "* [Solution Notebook](#Solution-Notebook)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Constraints\n", + "* Can we assume the string is ASCII?\n", + " * Yes\n", + "* Is whitespace important?\n", + " * no the whitespace does not change\n", + "* Is this case sensitive?\n", + " * yes\n", + "* What if the string is empty?\n", + " * return None\n", + "* Is the order of words important?\n", + " * yes\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm: Split words into a list and reverse each word individually\n", + "Steps:\n", + "\n", + "* Check if string is empty\n", + "* If not empty, split the string into a list of words \n", + "* For each word on the list\n", + " * reverse the word\n", + "* Return the string representation of the list\n", + "\n", + "Complexity:\n", + "\n", + "* Time complexity is O(n) where n is the number of chars.\n", + "* Space complexity is O(n) where n is the number of chars. " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def reverse_words(S):\n", + " if len(S) is 0:\n", + " return None\n", + " \n", + " words = S.split()\n", + " for i in range (len(words)):\n", + " words[i] = words[i][::-1]\n", + " \n", + " return \" \".join(words)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting reverse_words_solution.py\n" + ] + } + ], + "source": [ + "%%writefile reverse_words_solution.py\n", + "from nose.tools import assert_equal\n", + "\n", + "class UnitTest (object):\n", + " def testReverseWords(self, func):\n", + " assert_equal(func('the sun is hot'), 'eht nus si toh')\n", + " assert_equal(func(''), None)\n", + " assert_equal(func('123 456 789'), '321 654 987')\n", + " assert_equal(func('magic'), 'cigam')\n", + " print('Success: reverse_words')\n", + " \n", + "def main():\n", + " test = UnitTest()\n", + " test.testReverseWords(reverse_words)\n", + "\n", + "if __name__==\"__main__\":\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: reverse_words\n" + ] + } + ], + "source": [ + "run -i reverse_words_solution.py" + ] + } + ], + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/arrays_strings/reverse_words/reverse_words_solution.py b/arrays_strings/reverse_words/reverse_words_solution.py new file mode 100644 index 0000000..1e44114 --- /dev/null +++ b/arrays_strings/reverse_words/reverse_words_solution.py @@ -0,0 +1,16 @@ +from nose.tools import assert_equal + +class UnitTest (object): + def testReverseWords(self, func): + assert_equal(func('the sun is hot'), 'eht nus si toh') + assert_equal(func(''), None) + assert_equal(func('123 456 789'), '321 654 987') + assert_equal(func('magic'), 'cigam') + print('Success: reverse_words') + +def main(): + test = UnitTest() + test.testReverseWords(reverse_words) + +if __name__=="__main__": + main() \ No newline at end of file