diff --git a/05-Recursion/.ipynb_checkpoints/02-Recursion Homework Example Problems-checkpoint.ipynb b/05-Recursion/.ipynb_checkpoints/02-Recursion Homework Example Problems-checkpoint.ipynb index 60eae68..250e3d9 100644 --- a/05-Recursion/.ipynb_checkpoints/02-Recursion Homework Example Problems-checkpoint.ipynb +++ b/05-Recursion/.ipynb_checkpoints/02-Recursion Homework Example Problems-checkpoint.ipynb @@ -26,22 +26,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def rec_sum(n):\n", - " \n", - " pass" + "\n", + " # Base case \n", + " if n == 0:\n", + " return 0\n", + " else:\n", + "\n", + " return n + rec_sum(n-1)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "55" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "rec_sum(4)" + "rec_sum(10)" ] }, { @@ -57,19 +73,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def sum_func(n):\n", - " pass" + " # Base case \n", + " if len(str(n)) == 1:\n", + " \n", + " return n\n", + " else:\n", + " return (n%10) + sum_func(n//10)\n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "sum_func(4321)" ] @@ -103,7 +136,10 @@ { "cell_type": "markdown", "metadata": { - "collapsed": true + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } }, "source": [ "We'll need to think of this function recursively by knowing that:\n", @@ -125,39 +161,79 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ - "word_split('themanran',['the','ran','man'])" + "def word_split(phrase,list_of_words, output = None):\n", + " #base case.\n", + " if output is None:\n", + " output = []\n", + " for word in list_of_words:\n", + " if phrase.startswith(word):\n", + " output.append(word)\n", + " return word_split(phrase[len(word):], list_of_words, output)\n", + " return output " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['the', 'man', 'ran']" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "word_split('ilovedogsJohn',['i','am','a','dogs','lover','love','John'])" + "word_split('themanran',['the','ran','man'])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['i', 'love', 'dogs', 'John']" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "word_split('themanran',['clown','ran','man'])" + "word_split('ilovedogsJohn',['i','am','a','dogs','lover','love','John'])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "def word_split(phrase,list_of_words, output = None):\n", - " pass " + "word_split('themanran',['clown','ran','man'])" ] }, { @@ -172,7 +248,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -186,9 +262,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.12.1" } }, "nbformat": 4, - "nbformat_minor": 1 + "nbformat_minor": 4 } diff --git a/05-Recursion/02-Recursion Homework Example Problems.ipynb b/05-Recursion/02-Recursion Homework Example Problems.ipynb index 60eae68..250e3d9 100644 --- a/05-Recursion/02-Recursion Homework Example Problems.ipynb +++ b/05-Recursion/02-Recursion Homework Example Problems.ipynb @@ -26,22 +26,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def rec_sum(n):\n", - " \n", - " pass" + "\n", + " # Base case \n", + " if n == 0:\n", + " return 0\n", + " else:\n", + "\n", + " return n + rec_sum(n-1)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "55" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "rec_sum(4)" + "rec_sum(10)" ] }, { @@ -57,19 +73,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def sum_func(n):\n", - " pass" + " # Base case \n", + " if len(str(n)) == 1:\n", + " \n", + " return n\n", + " else:\n", + " return (n%10) + sum_func(n//10)\n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "sum_func(4321)" ] @@ -103,7 +136,10 @@ { "cell_type": "markdown", "metadata": { - "collapsed": true + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } }, "source": [ "We'll need to think of this function recursively by knowing that:\n", @@ -125,39 +161,79 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ - "word_split('themanran',['the','ran','man'])" + "def word_split(phrase,list_of_words, output = None):\n", + " #base case.\n", + " if output is None:\n", + " output = []\n", + " for word in list_of_words:\n", + " if phrase.startswith(word):\n", + " output.append(word)\n", + " return word_split(phrase[len(word):], list_of_words, output)\n", + " return output " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['the', 'man', 'ran']" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "word_split('ilovedogsJohn',['i','am','a','dogs','lover','love','John'])" + "word_split('themanran',['the','ran','man'])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['i', 'love', 'dogs', 'John']" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "word_split('themanran',['clown','ran','man'])" + "word_split('ilovedogsJohn',['i','am','a','dogs','lover','love','John'])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "def word_split(phrase,list_of_words, output = None):\n", - " pass " + "word_split('themanran',['clown','ran','man'])" ] }, { @@ -172,7 +248,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -186,9 +262,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.12.1" } }, "nbformat": 4, - "nbformat_minor": 1 + "nbformat_minor": 4 }