diff --git a/Graphs/Implementation of Depth First Search.ipynb b/Graphs/Implementation of Depth First Search.ipynb index 9551040f..4972f71b 100644 --- a/Graphs/Implementation of Depth First Search.ipynb +++ b/Graphs/Implementation of Depth First Search.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 1, "metadata": { "collapsed": true }, @@ -40,30 +40,44 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set(['A'])\n", + "set(['A', 'B'])\n", + "set(['A', 'B', 'D'])\n", + "set(['A', 'B', 'E', 'D'])\n", + "set(['A', 'B', 'E', 'D', 'F'])\n", + "set(['A', 'C', 'B', 'E', 'D', 'F'])\n" + ] + }, { "data": { "text/plain": [ "{'A', 'B', 'C', 'D', 'E', 'F'}" ] }, - "execution_count": 15, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def dfs(graph, start):\n", + " \"\"\" add adjacent vertices\"\"\"\n", " visited, stack = set(), [start]\n", " while stack:\n", " vertex = stack.pop()\n", " if vertex not in visited:\n", " visited.add(vertex)\n", " stack.extend(graph[vertex] - visited)\n", + " print(visited) #<-- this doesn't seem to be depth-first in the last 2 iterations?\n", " return visited\n", "\n", "dfs(graph, 'A') " @@ -73,23 +87,36 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The second implementation provides the same functionality as the first, however, this time we are using the more succinct recursive form. Due to a common Python gotcha with default parameter values being created only once, we are required to create a new visited set on each user invocation. Another Python language detail is that function variables are passed by reference, resulting in the visited mutable set not having to reassigned upon each recursive call." + "The second implementation provides the same functionality as the first, however, this time we are using the more succinct recursive form. *Due to a common Python gotcha with default parameter values being created only once, we are required to create a new visited set on each user invocation. Another Python language detail is that function variables are passed by reference, resulting in the visited mutable set not having to reassigned upon each recursive call.*" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set(['A'])\n", + "set(['A', 'C'])\n", + "set(['A', 'C', 'F'])\n", + "set(['A', 'C', 'E', 'F'])\n", + "set(['A', 'C', 'B', 'E', 'F'])\n", + "set(['A', 'C', 'B', 'E', 'D', 'F'])\n", + "set(['A', 'C', 'B', 'E', 'D', 'F'])\n" + ] + }, { "data": { "text/plain": [ "{'A', 'B', 'C', 'D', 'E', 'F'}" ] }, - "execution_count": 12, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -99,6 +126,7 @@ " if visited is None:\n", " visited = set()\n", " visited.add(start)\n", + " print(visited) #<-- this is also not looking like depth-first to me & it's different than the previous one?\n", " for nxt in graph[start] - visited:\n", " dfs(graph, nxt, visited)\n", " return visited\n", @@ -116,18 +144,30 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "C\n", + "B\n", + "E\n", + "D\n", + "F\n", + "F\n" + ] + }, { "data": { "text/plain": [ "[['A', 'B', 'E', 'F'], ['A', 'C', 'F']]" ] }, - "execution_count": 11, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -138,6 +178,7 @@ " while stack:\n", " (vertex, path) = stack.pop()\n", " for nxt in graph[vertex] - set(path):\n", + " print(nxt)\n", " if nxt == goal:\n", " yield path + [nxt]\n", " else:\n", @@ -176,7 +217,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.11" + "version": "2.7.10" } }, "nbformat": 4, diff --git a/Recursion/.ipynb_checkpoints/Introduction to Recursion-checkpoint.ipynb b/Recursion/.ipynb_checkpoints/Introduction to Recursion-checkpoint.ipynb index 0a165bd9..6dcbf5c0 100644 --- a/Recursion/.ipynb_checkpoints/Introduction to Recursion-checkpoint.ipynb +++ b/Recursion/.ipynb_checkpoints/Introduction to Recursion-checkpoint.ipynb @@ -23,7 +23,7 @@ "_______\n", "# Factorial Example\n", "\n", - "In this part of the lecture we will explain recursion through an example excercise of creating the factorial function.\n", + "In this part of the lecture we will explain recursion through an example exercise of creating the factorial function.\n", "The factorial function is denoted with an exclamation point and is defined as the product of the integers from 1 to *n*. Formally, we can state this as:\n", "\n", "$$ n! = n·(n-1)·(n-2)... 3·2·1 $$\n", diff --git a/Recursion/.ipynb_checkpoints/Memoization-checkpoint.ipynb b/Recursion/.ipynb_checkpoints/Memoization-checkpoint.ipynb index 7de1aee7..c1836bf7 100644 --- a/Recursion/.ipynb_checkpoints/Memoization-checkpoint.ipynb +++ b/Recursion/.ipynb_checkpoints/Memoization-checkpoint.ipynb @@ -22,6 +22,17 @@ "collapsed": true }, "outputs": [], + "source": [ + "import timeit" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], "source": [ "# Create cache for known results\n", "factorial_memo = {}\n", @@ -39,24 +50,22 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { - "data": { - "text/plain": [ - "24" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "The slowest run took 25.11 times longer than the fastest. This could mean that an intermediate result is being cached \n", + "1000000 loops, best of 3: 323 ns per loop\n" + ] } ], "source": [ - "factorial(4)" + "%timeit factorial(4)" ] }, { @@ -74,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "collapsed": true }, @@ -99,9 +108,9 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], "source": [ @@ -111,8 +120,27 @@ " return 1\n", " \n", " return k * factorial(k - 1)\n", - "\n", - "factorial = Memoize(factorial)" + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The slowest run took 7.22 times longer than the fastest. This could mean that an intermediate result is being cached \n", + "1000000 loops, best of 3: 693 ns per loop\n" + ] + } + ], + "source": [ + "%timeit Memoize(factorial)" ] }, { diff --git a/Recursion/.ipynb_checkpoints/Recursion Homework Example Problems-checkpoint.ipynb b/Recursion/.ipynb_checkpoints/Recursion Homework Example Problems-checkpoint.ipynb index ebe52213..5baa1da3 100644 --- a/Recursion/.ipynb_checkpoints/Recursion Homework Example Problems-checkpoint.ipynb +++ b/Recursion/.ipynb_checkpoints/Recursion Homework Example Problems-checkpoint.ipynb @@ -26,20 +26,68 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def rec_sum(n):\n", + " if n==0:\n", + " return n\n", + " else: \n", + " return n + rec_sum(n-1)\n", " \n", - " pass" + " " ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rec_sum(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rec_sum(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, "metadata": { "collapsed": false }, @@ -50,7 +98,7 @@ "10" ] }, - "execution_count": 8, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -72,19 +120,24 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def sum_func(n):\n", - " pass" + " \n", + " if len(str(n))==1:\n", + " return n\n", + " \n", + " else:\n", + " return int(str(n)[0]) + sum_func(int(str(n)[1:]))" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 14, "metadata": { "collapsed": false }, @@ -95,7 +148,7 @@ "10" ] }, - "execution_count": 4, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -172,7 +225,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", @@ -181,7 +234,53 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def word_split(phrase, list_of_words, output = None):\n", + " \"\"\" iterative version \"\"\"\n", + " word=\"\"\n", + " splitlist = []\n", + " for char in phrase:\n", + " word += char\n", + " if word in list_of_words:\n", + " splitlist.append(word)\n", + " word = \"\"\n", + " \n", + " return splitlist" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def word_split2(phrase, list_of_words, splitlist = None, word = \"\"):\n", + " \"\"\" recursive version \"\"\"\n", + " \n", + " if splitlist is None:\n", + " splitlist = []\n", + " \n", + " if len(phrase) == 0:\n", + " return splitlist\n", + " else:\n", + " word += phrase[0]\n", + " if word in list_of_words:\n", + " splitlist.append(word)\n", + " return word_split2(phrase[1:], list_of_words, splitlist)\n", + " else:\n", + " return word_split2(phrase[1:], list_of_words, splitlist, word)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, "metadata": { "collapsed": false }, @@ -192,18 +291,18 @@ "['the', 'man', 'ran']" ] }, - "execution_count": 32, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "word_split('themanran',['the','ran','man'])" + "word_split2('themanran',['the','ran','man'])" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 37, "metadata": { "collapsed": false }, @@ -214,18 +313,18 @@ "['i', 'love', 'dogs', 'John']" ] }, - "execution_count": 33, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "word_split('ilovedogsJohn',['i','am','a','dogs','lover','love','John'])" + "word_split2('ilovedogsJohn',['i','am','a','dogs','lover','love','John'])" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 38, "metadata": { "collapsed": false }, @@ -236,25 +335,13 @@ "[]" ] }, - "execution_count": 34, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "word_split('themanran',['clown','ran','man'])" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def word_split(phrase,list_of_words, output = None):\n", - " pass " + "word_split2('themanran',['clown','ran','man'])" ] }, { diff --git a/Recursion/Introduction to Recursion.ipynb b/Recursion/Introduction to Recursion.ipynb index 0a165bd9..6dcbf5c0 100644 --- a/Recursion/Introduction to Recursion.ipynb +++ b/Recursion/Introduction to Recursion.ipynb @@ -23,7 +23,7 @@ "_______\n", "# Factorial Example\n", "\n", - "In this part of the lecture we will explain recursion through an example excercise of creating the factorial function.\n", + "In this part of the lecture we will explain recursion through an example exercise of creating the factorial function.\n", "The factorial function is denoted with an exclamation point and is defined as the product of the integers from 1 to *n*. Formally, we can state this as:\n", "\n", "$$ n! = n·(n-1)·(n-2)... 3·2·1 $$\n", diff --git a/Recursion/Memoization.ipynb b/Recursion/Memoization.ipynb index 7de1aee7..c1836bf7 100644 --- a/Recursion/Memoization.ipynb +++ b/Recursion/Memoization.ipynb @@ -22,6 +22,17 @@ "collapsed": true }, "outputs": [], + "source": [ + "import timeit" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], "source": [ "# Create cache for known results\n", "factorial_memo = {}\n", @@ -39,24 +50,22 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { - "data": { - "text/plain": [ - "24" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "The slowest run took 25.11 times longer than the fastest. This could mean that an intermediate result is being cached \n", + "1000000 loops, best of 3: 323 ns per loop\n" + ] } ], "source": [ - "factorial(4)" + "%timeit factorial(4)" ] }, { @@ -74,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "collapsed": true }, @@ -99,9 +108,9 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], "source": [ @@ -111,8 +120,27 @@ " return 1\n", " \n", " return k * factorial(k - 1)\n", - "\n", - "factorial = Memoize(factorial)" + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The slowest run took 7.22 times longer than the fastest. This could mean that an intermediate result is being cached \n", + "1000000 loops, best of 3: 693 ns per loop\n" + ] + } + ], + "source": [ + "%timeit Memoize(factorial)" ] }, { diff --git a/Recursion/Recursion Homework Example Problems.ipynb b/Recursion/Recursion Homework Example Problems.ipynb index ebe52213..5baa1da3 100644 --- a/Recursion/Recursion Homework Example Problems.ipynb +++ b/Recursion/Recursion Homework Example Problems.ipynb @@ -26,20 +26,68 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def rec_sum(n):\n", + " if n==0:\n", + " return n\n", + " else: \n", + " return n + rec_sum(n-1)\n", " \n", - " pass" + " " ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rec_sum(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rec_sum(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, "metadata": { "collapsed": false }, @@ -50,7 +98,7 @@ "10" ] }, - "execution_count": 8, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -72,19 +120,24 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def sum_func(n):\n", - " pass" + " \n", + " if len(str(n))==1:\n", + " return n\n", + " \n", + " else:\n", + " return int(str(n)[0]) + sum_func(int(str(n)[1:]))" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 14, "metadata": { "collapsed": false }, @@ -95,7 +148,7 @@ "10" ] }, - "execution_count": 4, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -172,7 +225,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", @@ -181,7 +234,53 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def word_split(phrase, list_of_words, output = None):\n", + " \"\"\" iterative version \"\"\"\n", + " word=\"\"\n", + " splitlist = []\n", + " for char in phrase:\n", + " word += char\n", + " if word in list_of_words:\n", + " splitlist.append(word)\n", + " word = \"\"\n", + " \n", + " return splitlist" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def word_split2(phrase, list_of_words, splitlist = None, word = \"\"):\n", + " \"\"\" recursive version \"\"\"\n", + " \n", + " if splitlist is None:\n", + " splitlist = []\n", + " \n", + " if len(phrase) == 0:\n", + " return splitlist\n", + " else:\n", + " word += phrase[0]\n", + " if word in list_of_words:\n", + " splitlist.append(word)\n", + " return word_split2(phrase[1:], list_of_words, splitlist)\n", + " else:\n", + " return word_split2(phrase[1:], list_of_words, splitlist, word)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, "metadata": { "collapsed": false }, @@ -192,18 +291,18 @@ "['the', 'man', 'ran']" ] }, - "execution_count": 32, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "word_split('themanran',['the','ran','man'])" + "word_split2('themanran',['the','ran','man'])" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 37, "metadata": { "collapsed": false }, @@ -214,18 +313,18 @@ "['i', 'love', 'dogs', 'John']" ] }, - "execution_count": 33, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "word_split('ilovedogsJohn',['i','am','a','dogs','lover','love','John'])" + "word_split2('ilovedogsJohn',['i','am','a','dogs','lover','love','John'])" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 38, "metadata": { "collapsed": false }, @@ -236,25 +335,13 @@ "[]" ] }, - "execution_count": 34, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "word_split('themanran',['clown','ran','man'])" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def word_split(phrase,list_of_words, output = None):\n", - " pass " + "word_split2('themanran',['clown','ran','man'])" ] }, { 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..d5c5857f 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 @@ -17,7 +17,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -25,12 +25,16 @@ "source": [ "def reverse(s):\n", " \n", - " pass" + " if len(s) == 1:\n", + " return s\n", + " else:\n", + " return s[-1] + reverse(s[0:-1])\n", + " " ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -41,7 +45,7 @@ "'dlrow olleh'" ] }, - "execution_count": 10, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -67,7 +71,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 3, "metadata": { "collapsed": false }, diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 2 - String Permutation-checkpoint.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 2 - String Permutation-checkpoint.ipynb index e2585c12..25aabf33 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 2 - String Permutation-checkpoint.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 2 - String Permutation-checkpoint.ipynb @@ -31,7 +31,9 @@ "outputs": [], "source": [ "def permute(s):\n", + " \"\"\" try iteratively first \"\"\"\n", " \n", + " if \n", " pass" ] }, 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..d5c5857f 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 @@ -17,7 +17,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -25,12 +25,16 @@ "source": [ "def reverse(s):\n", " \n", - " pass" + " if len(s) == 1:\n", + " return s\n", + " else:\n", + " return s[-1] + reverse(s[0:-1])\n", + " " ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -41,7 +45,7 @@ "'dlrow olleh'" ] }, - "execution_count": 10, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -67,7 +71,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 3, "metadata": { "collapsed": false }, 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..5a91ea81 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,20 +24,102 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ - "def permute(s):\n", + "def permute(s, result=None):\n", " \n", - " pass" + " if len(s) == 1:\n", + " return s\n", + " else:\n", + " slist = [x for x in s]\n", + " return s + ''.join(np.roll(slist, 1))\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'b', 'c']" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list('abc')" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'ba'" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "''.join(np.roll([\"a\",\"b\"], 1))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'a': 0, 'b': 1}" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "letters" ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 21, "metadata": { "collapsed": false }, @@ -45,10 +127,10 @@ { "data": { "text/plain": [ - "['abc', 'acb', 'bac', 'bca', 'cab', 'cba']" + "'abccab'" ] }, - "execution_count": 24, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" }