{ "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 }