diff --git a/Algorithm Analysis and Big O/.ipynb_checkpoints/Big O Notation-checkpoint.ipynb b/Algorithm Analysis and Big O/.ipynb_checkpoints/Big O Notation-checkpoint.ipynb index 28cb4211..e360d571 100644 --- a/Algorithm Analysis and Big O/.ipynb_checkpoints/Big O Notation-checkpoint.ipynb +++ b/Algorithm Analysis and Big O/.ipynb_checkpoints/Big O Notation-checkpoint.ipynb @@ -24,7 +24,7 @@ " Take an input of n and return the sum of the numbers from 0 to n\n", " '''\n", " final_sum = 0\n", - " for x in xrange(n+1): \n", + " for x in range(n+1): \n", " final_sum += x\n", " \n", " return final_sum" @@ -121,9 +121,7 @@ { "cell_type": "code", "execution_count": 43, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -184,9 +182,9 @@ "metadata": { "anaconda-cloud": {}, "kernelspec": { - "display_name": "Python [conda env:python3]", + "display_name": "Python 3", "language": "python", - "name": "conda-env-python3-py" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -198,9 +196,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.6.5" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Algorithm Analysis and Big O/Big O Examples .ipynb b/Algorithm Analysis and Big O/Big O Examples .ipynb index 9352c727..c569065a 100644 --- a/Algorithm Analysis and Big O/Big O Examples .ipynb +++ b/Algorithm Analysis and Big O/Big O Examples .ipynb @@ -550,7 +550,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.5" } }, "nbformat": 4, diff --git a/Algorithm Analysis and Big O/Big O Notation.ipynb b/Algorithm Analysis and Big O/Big O Notation.ipynb index 28cb4211..e360d571 100644 --- a/Algorithm Analysis and Big O/Big O Notation.ipynb +++ b/Algorithm Analysis and Big O/Big O Notation.ipynb @@ -24,7 +24,7 @@ " Take an input of n and return the sum of the numbers from 0 to n\n", " '''\n", " final_sum = 0\n", - " for x in xrange(n+1): \n", + " for x in range(n+1): \n", " final_sum += x\n", " \n", " return final_sum" @@ -121,9 +121,7 @@ { "cell_type": "code", "execution_count": 43, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -184,9 +182,9 @@ "metadata": { "anaconda-cloud": {}, "kernelspec": { - "display_name": "Python [conda env:python3]", + "display_name": "Python 3", "language": "python", - "name": "conda-env-python3-py" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -198,9 +196,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.6.5" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Algorithm Analysis and Big O/Big O for Python Data Structures.ipynb b/Algorithm Analysis and Big O/Big O for Python Data Structures.ipynb index dd014176..073ce174 100644 --- a/Algorithm Analysis and Big O/Big O for Python Data Structures.ipynb +++ b/Algorithm Analysis and Big O/Big O for Python Data Structures.ipynb @@ -276,7 +276,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.5" } }, "nbformat": 4, diff --git a/Algorithm Analysis and Big O/Introduction to Algorithm Analysis and Big O .ipynb b/Algorithm Analysis and Big O/Introduction to Algorithm Analysis and Big O .ipynb index 0238db22..3f933e56 100644 --- a/Algorithm Analysis and Big O/Introduction to Algorithm Analysis and Big O .ipynb +++ b/Algorithm Analysis and Big O/Introduction to Algorithm Analysis and Big O .ipynb @@ -24,10 +24,8 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, + "execution_count": 3, + "metadata": {}, "outputs": [], "source": [ "# First function (Note the use of xrange since this is in Python 2)\n", @@ -36,7 +34,7 @@ " Take an input of n and return the sum of the numbers from 0 to n\n", " '''\n", " final_sum = 0\n", - " for x in xrange(n+1): \n", + " for x in range(n+1): \n", " final_sum += x\n", " \n", " return final_sum" @@ -44,10 +42,8 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": 4, + "metadata": {}, "outputs": [ { "data": { @@ -55,7 +51,7 @@ "55" ] }, - "execution_count": 5, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -66,10 +62,8 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, + "execution_count": 5, + "metadata": {}, "outputs": [], "source": [ "def sum2(n):\n", @@ -81,18 +75,16 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 6, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "55" + "55.0" ] }, - "execution_count": 9, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -115,17 +107,14 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The slowest run took 5.15 times longer than the fastest. This could mean that an intermediate result is being cached \n", - "100000 loops, best of 3: 4.86 µs per loop\n" + "5.78 µs ± 346 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], @@ -135,17 +124,14 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 8, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The slowest run took 16.54 times longer than the fastest. This could mean that an intermediate result is being cached \n", - "10000000 loops, best of 3: 173 ns per loop\n" + "171 ns ± 1.42 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\n" ] } ], @@ -172,23 +158,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.5" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Practice.ipynb b/Practice.ipynb index b2ecf2ed..5c4ef0ec 100644 --- a/Practice.ipynb +++ b/Practice.ipynb @@ -1,8 +1,15 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "try shift+tab with a python method/object to see the documentation" + ] + }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -11,7 +18,7 @@ "'HELLO'" ] }, - "execution_count": 5, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } @@ -21,6 +28,26 @@ "s.upper()" ] }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.capitalize()" + ] + }, { "cell_type": "code", "execution_count": 8, @@ -1399,7 +1426,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/Recursion/Introduction to Recursion.ipynb b/Recursion/Introduction to Recursion.ipynb index 0a165bd9..f43eb751 100644 --- a/Recursion/Introduction to Recursion.ipynb +++ b/Recursion/Introduction to Recursion.ipynb @@ -58,9 +58,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def fact(n):\n", @@ -87,9 +85,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -118,9 +114,7 @@ { "cell_type": "code", "execution_count": 10, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -158,23 +152,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.5" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Memoization.ipynb b/Recursion/Memoization.ipynb index 8921ed05..19f25c13 100644 --- a/Recursion/Memoization.ipynb +++ b/Recursion/Memoization.ipynb @@ -137,7 +137,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.5" } }, "nbformat": 4, diff --git a/Recursion/Recursion Homework Example Problems - SOLUTIONS.ipynb b/Recursion/Recursion Homework Example Problems - SOLUTIONS.ipynb index f7bafd8e..de09f5c8 100644 --- a/Recursion/Recursion Homework Example Problems - SOLUTIONS.ipynb +++ b/Recursion/Recursion Homework Example Problems - SOLUTIONS.ipynb @@ -47,9 +47,7 @@ { "cell_type": "code", "execution_count": 8, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -98,9 +96,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -127,9 +123,7 @@ { "cell_type": "code", "execution_count": 13, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -150,9 +144,7 @@ { "cell_type": "code", "execution_count": 14, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -195,9 +187,7 @@ { "cell_type": "code", "execution_count": 32, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -217,9 +207,7 @@ { "cell_type": "code", "execution_count": 33, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -239,9 +227,7 @@ { "cell_type": "code", "execution_count": 34, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -261,9 +247,7 @@ { "cell_type": "code", "execution_count": 24, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def word_split(phrase,list_of_words, output = None):\n", @@ -305,23 +289,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.5" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Recursion Homework Example Problems.ipynb b/Recursion/Recursion Homework Example Problems.ipynb index ebe52213..6dd86673 100644 --- a/Recursion/Recursion Homework Example Problems.ipynb +++ b/Recursion/Recursion Homework Example Problems.ipynb @@ -26,23 +26,21 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "def rec_sum(n):\n", " \n", - " pass" + " if n == 0:\n", + " return 0\n", + " return n + rec_sum(n-1)" ] }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 2, + "metadata": {}, "outputs": [ { "data": { @@ -50,7 +48,7 @@ "10" ] }, - "execution_count": 8, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -59,6 +57,26 @@ "rec_sum(4)" ] }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "55" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rec_sum(10)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -72,36 +90,54 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, + "execution_count": 9, + "metadata": {}, "outputs": [], "source": [ "def sum_func(n):\n", - " pass" + " if n == 0: \n", + " return 0\n", + " return (n % 10) + sum_func(int(n/10))" ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "10" + "6" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum_func(111111)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "432.1" ] }, - "execution_count": 4, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "sum_func(4321)" + "4321/10" ] }, { @@ -114,9 +150,7 @@ { "cell_type": "code", "execution_count": 13, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -137,9 +171,7 @@ { "cell_type": "code", "execution_count": 14, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -172,7 +204,7 @@ "source": [ "________\n", "### Problem 3\n", - "*Note, this is a more advanced problem than the previous two! It aso has a lot of variation possibilities and we're ignoring strict requirements here.*\n", + "*Note, this is a more advanced problem than the previous two! It also has a lot of variation possibilities and we're ignoring strict requirements here.*\n", "\n", "Create a function called word_split() which takes in a string **phrase** and a set **list_of_words**. The function will then determine if it is possible to split the string in a way in which words can be made from the list of words. You can assume the phrase will only contain words found in the dictionary if it is completely splittable.\n", "\n", @@ -182,9 +214,7 @@ { "cell_type": "code", "execution_count": 32, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -204,9 +234,7 @@ { "cell_type": "code", "execution_count": 33, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -226,9 +254,7 @@ { "cell_type": "code", "execution_count": 34, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -247,14 +273,83 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, + "execution_count": 37, + "metadata": {}, "outputs": [], "source": [ - "def word_split(phrase,list_of_words, output = None):\n", - " pass " + "def word_split(phrase, list_of_words, output=None):\n", + " print(phrase)\n", + " if output == None:\n", + " output = []\n", + " if len(phrase) == 0:\n", + " return output\n", + " for word in list_of_words:\n", + " if phrase.find(word) == 0:\n", + " output.append(word)\n", + " return word_split(phrase[len(word):], list_of_words, output)\n", + " return output\n", + "\n", + "# wrong\n", + "def word_split_wrong(phrase,list_of_words, output = None):\n", + " if output == None:\n", + " output = []\n", + " if len(list_of_words) == 0:\n", + " return output\n", + " word = list_of_words[0]\n", + " if phrase.find(word) > -1:\n", + " output.append(word)\n", + " return word_split(phrase, list_of_words[1:], output)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "themanran\n" + ] + }, + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "word_split('themanran',['clown','ran','man'])" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "foo = 'helloworld'\n", + "foo.find('hello')\n", + "foo= ''\n", + "len(foo)" ] }, { @@ -269,23 +364,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.5" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 1 - Reverse String - SOLUTION.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 1 - Reverse String - SOLUTION.ipynb index fa3aca61..f12f2537 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 1 - Reverse String - SOLUTION.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 1 - Reverse String - SOLUTION.ipynb @@ -132,7 +132,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 2 - String Permutation- SOLUTION.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 2 - String Permutation- SOLUTION.ipynb index a45943a5..27a3e71f 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 2 - String Permutation- SOLUTION.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 2 - String Permutation- SOLUTION.ipynb @@ -163,7 +163,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.5" } }, "nbformat": 4, diff --git a/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 3 - Fibonacci Sequence - SOLUTION.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 3 - Fibonacci Sequence - SOLUTION.ipynb index a774e188..fef7e3bc 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 3 - Fibonacci Sequence - SOLUTION.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 3 - Fibonacci Sequence - SOLUTION.ipynb @@ -33,9 +33,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "def fib_rec(n):\n", @@ -136,10 +134,8 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, + "execution_count": 4, + "metadata": {}, "outputs": [], "source": [ "def fib_iter(n):\n", @@ -158,22 +154,22 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "28657" + "2" ] }, - "execution_count": 14, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "fib_iter(23)" + "fib_iter(3)" ] }, { @@ -247,7 +243,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.5" } }, "nbformat": 4, diff --git a/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 4 - Coin Change - SOLUTION.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 4 - Coin Change - SOLUTION.ipynb index 2368b86b..0da8db5e 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 4 - Coin Change - SOLUTION.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 4 - Coin Change - SOLUTION.ipynb @@ -291,7 +291,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 1 - Reverse String -checkpoint.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 1 - Reverse String -checkpoint.ipynb index 4d4308f6..0c93dd14 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 1 - Reverse String -checkpoint.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 1 - Reverse String -checkpoint.ipynb @@ -18,22 +18,41 @@ { "cell_type": "code", "execution_count": 7, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def reverse(s):\n", " \n", - " pass" + " if len(s) == 0:\n", + " return ''\n", + " return s[len(s)-1] + reverse(s[:-1])" ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "foo = [1,2,3,4,5]\n", + "foo[:-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, "outputs": [ { "data": { @@ -41,7 +60,7 @@ "'dlrow olleh'" ] }, - "execution_count": 10, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -67,10 +86,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 10, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -94,7 +111,7 @@ " assert_equal(solution('hello world'),'dlrow olleh')\n", " assert_equal(solution('123456789'),'987654321')\n", " \n", - " print 'PASSED ALL TEST CASES!'\n", + " print ('PASSED ALL TEST CASES!')\n", " \n", "# Run Tests\n", "test = TestReverse()\n", @@ -111,23 +128,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.5" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 1 - Reverse String .ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 1 - Reverse String .ipynb index 4d4308f6..0c93dd14 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 1 - Reverse String .ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 1 - Reverse String .ipynb @@ -18,22 +18,41 @@ { "cell_type": "code", "execution_count": 7, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def reverse(s):\n", " \n", - " pass" + " if len(s) == 0:\n", + " return ''\n", + " return s[len(s)-1] + reverse(s[:-1])" ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "foo = [1,2,3,4,5]\n", + "foo[:-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, "outputs": [ { "data": { @@ -41,7 +60,7 @@ "'dlrow olleh'" ] }, - "execution_count": 10, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -67,10 +86,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 10, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -94,7 +111,7 @@ " assert_equal(solution('hello world'),'dlrow olleh')\n", " assert_equal(solution('123456789'),'987654321')\n", " \n", - " print 'PASSED ALL TEST CASES!'\n", + " print ('PASSED ALL TEST CASES!')\n", " \n", "# Run Tests\n", "test = TestReverse()\n", @@ -111,23 +128,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.5" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 2 - String Permutation.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 2 - String Permutation.ipynb index e2585c12..bb073a78 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 2 - String Permutation.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 2 - String Permutation.ipynb @@ -24,37 +24,128 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, + "execution_count": 25, + "metadata": {}, "outputs": [], "source": [ "def permute(s):\n", + " if len(s) == 1:\n", + " return s\n", + "\n", + "def permute(s):\n", + " out = []\n", " \n", - " pass" + " # Base Case\n", + " if len(s) == 1:\n", + " out = [s]\n", + " print('out=',out, '(base)')\n", + " \n", + " else:\n", + " # For every letter in string\n", + " for i, let in enumerate(s):\n", + " \n", + " # The slice passed to permute does NOT contain the current letter 'let' \n", + " # For every permutation resulting from Step 2 and 3 described above\n", + " for perm in permute(s[:i] + s[i+1:]):\n", + " \n", + " # Add it to output\n", + " out += [let + perm]\n", + " print('out=',out)\n", + "\n", + " return out\n", + " " ] }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['abc', 'bac']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['abc'] + ['bac']" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "i= 0 let= a s[:i]= s[i+1:]= b\n", + "i= 1 let= b s[:i]= a s[i+1:]= \n" + ] + } + ], + "source": [ + "stuff = 'ab'\n", + "for i, let in enumerate(stuff):\n", + " print('i=',i,'let=',let, 's[:i]=', stuff[:i], 's[i+1:]=', stuff[i+1:])\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Easier to comprehend with a two letter string\n", + "\n", + "1. For every letter in the string - start with 'a'\n", + "1. Hold on to 'a'\n", + " 1. Create an new string withOUT the current letter 'a'\n", + " 1. Pass it into permute (e.g. `permute('b')`) \n", + " 1. 'b' is one letter, so an array of `['b']` is returned\n", + " 1. iterate over of the array, joining the letter 'a' to 'b' and adding it to the `out` array\n", + " 1. the array now contains `['ab']`\n", + "1. Next, 'b' - hold on to that.\n", + " 1. Create a new string withOUT the 'b' - so now 'a'\n", + " 1. Pass to permute. This is the base case, so `['a']` is returned\n", + " 1. iterate over the single element array, joining 'b' to 'a' and pushing it to the `out` array\n", + " 1. the array now contains `['ab', 'ba']`" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "out= ['1'] (base)\n", + "out= ['01']\n", + "out= ['0'] (base)\n", + "out= ['01', '10']\n" + ] + }, { "data": { "text/plain": [ - "['abc', 'acb', 'bac', 'bca', 'cab', 'cba']" + "2" ] }, - "execution_count": 24, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "permute('abc')" + "\n", + "what = permute('01')\n", + "len(what)" ] }, { @@ -68,9 +159,7 @@ { "cell_type": "code", "execution_count": 25, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -113,23 +202,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.5" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 3 - Fibonacci Sequence.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 3 - Fibonacci Sequence.ipynb index e85c24d0..8b835bdc 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 3 - Fibonacci Sequence.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 3 - Fibonacci Sequence.ipynb @@ -41,23 +41,20 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 11, + "metadata": {}, "outputs": [], "source": [ "def fib_rec(n):\n", - " \n", - " pass" + " if n <= 2:\n", + " return 1\n", + " return fib_rec(n-1) + fib_rec(n-2)" ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, "outputs": [ { "data": { @@ -65,7 +62,7 @@ "55" ] }, - "execution_count": 6, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -85,36 +82,45 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 31, + "metadata": {}, "outputs": [], "source": [ "# Instantiate Cache information\n", - "n = 10\n", - "cache = [None] * (n + 1)\n", - "\n", + "# n = 10\n", + "# cache = [None] * (n + 1)\n", + "cache = {}\n", "\n", "def fib_dyn(n):\n", - " \n", - " pass" + " if n in cache:\n", + " print('used cached!', n)\n", + " return cache[n]\n", + " if n <=2:\n", + " return 1\n", + " result = fib_dyn(n-1) + fib_dyn(n-2)\n", + " cache[n] = result\n", + " return result\n" ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 33, + "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "used cached! 10\n" + ] + }, { "data": { "text/plain": [ "55" ] }, - "execution_count": 9, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -123,6 +129,26 @@ "fib_dyn(10)" ] }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55}" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cache" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -134,23 +160,28 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, + "execution_count": 38, + "metadata": {}, "outputs": [], "source": [ "def fib_iter(n):\n", + " if n <= 2:\n", + " return 1\n", + " val = 0\n", + " prev1 = 1\n", + " prev2 = 1\n", + " for i in range(n-2):\n", + " val = prev1 + prev2\n", + " prev2 = prev1\n", + " prev1 = val\n", " \n", - " pass" + " return val\n" ] }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 39, + "metadata": {}, "outputs": [ { "data": { @@ -158,7 +189,7 @@ "28657" ] }, - "execution_count": 14, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } @@ -178,10 +209,8 @@ }, { "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, + "execution_count": 40, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -205,13 +234,53 @@ " assert_equal(solution(10),55)\n", " assert_equal(solution(1),1)\n", " assert_equal(solution(23),28657)\n", - " print 'Passed all tests.'\n", + " print ('Passed all tests.')\n", "# UNCOMMENT FOR CORRESPONDING FUNCTION\n", "t = TestFib()\n", "\n", - "t.test(fib_rec)\n", - "#t.test(fib_dyn) # Note, will need to reset cache size for each test!\n", - "#t.test(fib_iter)" + "# t.test(fib_rec)\n", + "# t.test(fib_dyn) # Note, will need to reset cache size for each test!\n", + "t.test(fib_iter)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{3: 2,\n", + " 4: 3,\n", + " 5: 5,\n", + " 6: 8,\n", + " 7: 13,\n", + " 8: 21,\n", + " 9: 34,\n", + " 10: 55,\n", + " 11: 89,\n", + " 12: 144,\n", + " 13: 233,\n", + " 14: 377,\n", + " 15: 610,\n", + " 16: 987,\n", + " 17: 1597,\n", + " 18: 2584,\n", + " 19: 4181,\n", + " 20: 6765,\n", + " 21: 10946,\n", + " 22: 17711,\n", + " 23: 28657}" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cache" ] }, { @@ -226,23 +295,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.5" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 4 - Coin Change.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 4 - Coin Change.ipynb index f8785dfb..bbc58adf 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 4 - Coin Change.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 4 - Coin Change.ipynb @@ -37,6 +37,49 @@ "Implement your solution below:" ] }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 5, 10]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "target = 10\n", + "coins = [1,5,10]\n", + "[c for c in coins if c <= target]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "foo = [1,2,3]\n", + "for i in range(0,len(foo)):\n", + " print(foo[i])" + ] + }, { "cell_type": "code", "execution_count": 2, @@ -46,16 +89,20 @@ "outputs": [], "source": [ "def rec_coin(target,coins):\n", - " \n", - " pass" + " if len(coins)==0:\n", + " return 0\n", + " if len(coins) == 1:\n", + " return target/coins[0]\n", + " solutions = 0\n", + " for i in range(0,len(coins)):\n", + " \n", + " \n" ] }, { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -86,9 +133,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", @@ -125,23 +170,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.6" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 }