From 15f8ff88f0508a2f2e2f2c39744c76f40dfe34c6 Mon Sep 17 00:00:00 2001 From: eamanu Date: Thu, 27 Apr 2017 21:00:56 -0300 Subject: [PATCH] Add solution to challenge reverse words --- .../reverse_words_challenge.ipynb | 19 +- .../reverse_words/reverse_words_challenge.py | 37 ++++ .../reverse_words_challenge_solution.ipynb | 187 ++++++++++++++++++ .../reverse_words/test_reverse_words.py | 22 +++ 4 files changed, 250 insertions(+), 15 deletions(-) create mode 100644 arrays_strings/reverse_words/reverse_words_challenge.py create mode 100644 arrays_strings/reverse_words/reverse_words_challenge_solution.ipynb create mode 100644 arrays_strings/reverse_words/test_reverse_words.py diff --git a/arrays_strings/reverse_words/reverse_words_challenge.ipynb b/arrays_strings/reverse_words/reverse_words_challenge.ipynb index e79354c..67cfa16 100644 --- a/arrays_strings/reverse_words/reverse_words_challenge.ipynb +++ b/arrays_strings/reverse_words/reverse_words_challenge.ipynb @@ -92,16 +92,6 @@ "## Unit Test" ] }, - { - "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, - "source": [ - "**The following unit test is expected to fail until you solve the challenge.**" - ] - }, { "cell_type": "code", "execution_count": null, @@ -110,8 +100,7 @@ }, "outputs": [], "source": [ - "\n", - "# %load test_reverse_string.py\n", + "# %load test_reverse_words.py\n", "from nose.tools import assert_equal\n", "\n", "\n", @@ -140,9 +129,9 @@ "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 2", "language": "python", - "name": "python3" + "name": "python2" }, "language_info": { "codemirror_mode": { @@ -154,7 +143,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.9" + "version": "2.7.12" } }, "nbformat": 4, diff --git a/arrays_strings/reverse_words/reverse_words_challenge.py b/arrays_strings/reverse_words/reverse_words_challenge.py new file mode 100644 index 0000000..ac06573 --- /dev/null +++ b/arrays_strings/reverse_words/reverse_words_challenge.py @@ -0,0 +1,37 @@ +class ReverseWords: + + def reverse(self, string): + if string is None: + return None + if string == ['']: + return [''] + buff_w = [] # buffer for substrings + substring = [] # substring + new_string = [] # new string with reverses words + for i in range(len(string)): + if(string[i] == ' '): + for h in range(len(buff_w) - 1, -1, -1): + substring.append(buff_w[h]) + for j in range(len(substring)): + new_string.append(substring[j]) + substring = [] + buff_w = [] + else: + buff_w.append(string[i]) + if(i == (len(string) - 1)): + new_string.append(' ') + for h in range(len(buff_w) - 1, -1, -1): + substring.append(buff_w[h]) + for j in range(len(substring)): + new_string.append(substring[j]) + return new_string + + +def main(): + R = ReverseWords() + result = R.reverse('foo bar') + print(result) + + +if __name__ == "__main__": + main() diff --git a/arrays_strings/reverse_words/reverse_words_challenge_solution.ipynb b/arrays_strings/reverse_words/reverse_words_challenge_solution.ipynb new file mode 100644 index 0000000..4098e1c --- /dev/null +++ b/arrays_strings/reverse_words/reverse_words_challenge_solution.ipynb @@ -0,0 +1,187 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [eamanu](http://github.com/eamanu). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Implement a function to reverse words within a string. \n", + "\n", + "* [Constraints](#Constraints)\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", + "\n", + "* Can we assume the string is ASCII?\n", + " * Yes\n", + " * Note: Unicode strings could require special handling depending on your language\n", + "* Since we need to do this in-place, it seems we cannot use the slice operator or the reversed function?\n", + " * Correct\n", + "* Since Python string are immutable, can we use a list of characters instead?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* None -> None\n", + "* [''] -> ['']\n", + "* ['f', 'o', 'o', ' ', 'b', 'a', 'r'] -> ['o', 'o', 'f', ' ', 'r', 'a', 'b']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "* First if String is None\n", + " * Return None\n", + "* If String is ['']\n", + " * Return ['']\n", + "* Go throught the string and we append in a buffer\n", + "* IF the actual char is ' ' we reverse the buffer and put into a new string (the solution)\n", + "* Repeat until the finished.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class ReverseWords:\n", + "\n", + " def reverse(self, string):\n", + " if string is None:\n", + " return None\n", + " if string == ['']:\n", + " return ['']\n", + " buff_w = [] # buffer for substrings\n", + " substring = [] # substring\n", + " new_string = [] # new string with reverses words\n", + " for i in range(len(string)):\n", + " if(string[i] == ' '):\n", + " for h in range(len(buff_w) - 1, -1, -1):\n", + " substring.append(buff_w[h])\n", + " for j in range(len(substring)):\n", + " new_string.append(substring[j])\n", + " substring = []\n", + " buff_w = []\n", + " else:\n", + " buff_w.append(string[i])\n", + " if(i == (len(string) - 1)):\n", + " new_string.append(' ')\n", + " for h in range(len(buff_w) - 1, -1, -1):\n", + " substring.append(buff_w[h])\n", + " for j in range(len(substring)):\n", + " new_string.append(substring[j])\n", + " return new_string\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_reverse\n" + ] + } + ], + "source": [ + "# %load test_reverse_words.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestReverseWords(object):\n", + "\n", + " def test_reverse(self, func):\n", + " assert_equal(func(None), None)\n", + " assert_equal(func(['']), [''])\n", + " assert_equal(func(\n", + " ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n", + " ['o', 'o', 'f', ' ', 'r', 'a', 'b'])\n", + " print('Success: test_reverse')\n", + " \n", + " \n", + "def main():\n", + " test = TestReverseWords()\n", + " reverse_string = ReverseWords()\n", + " test.test_reverse(reverse_string.reverse)\n", + "\n", + " \n", + "if __name__ == '__main__':\n", + " main()" + ] + } + ], + "metadata": { + "celltoolbar": "Edit Metadata", + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/arrays_strings/reverse_words/test_reverse_words.py b/arrays_strings/reverse_words/test_reverse_words.py new file mode 100644 index 0000000..1580f47 --- /dev/null +++ b/arrays_strings/reverse_words/test_reverse_words.py @@ -0,0 +1,22 @@ +from nose.tools import assert_equal + + +class TestReverseWords(object): + + def test_reverse(self, func): + assert_equal(func(None), None) + assert_equal(func(['']), ['']) + assert_equal(func( + ['f', 'o', 'o', ' ', 'b', 'a', 'r']), + ['o', 'o', 'f', ' ', 'r', 'a', 'b']) + print('Success: test_reverse') + + +def main(): + test = TestReverseWords() + reverse_string = ReverseWords() + test.test_reverse(reverse_string.reverse) + + +if __name__ == '__main__': + main()