{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sebastian Raschka \n", "last updated: 2017-08-03 \n", "\n", "CPython 3.6.1\n", "IPython 6.1.0\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -a 'Sebastian Raschka' -u -d -v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Examples using Recursion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Important Note**\n", "\n", "For most cases, using function recursion should be avoided in Python are better be implemented using for/while loops most of the time (although, I must admit that recursive solutions do look elegant). One of the reasons is that stacking recursive calls can easily blow up memory or at least result in the popular yet nasty \"RuntimeError: maximum recursion depth exceeded\". Also, keep in mind that Python does not optimize tail recursion in favor of having the full tracebacks for debugging; related to that, please see Guido van Rossums blog posts \"[Tail Recursion Elimination](http://neopythonic.blogspot.com.au/2009/04/tail-recursion-elimination.html)\" and \"[Final Words on Tail Calls](http://neopythonic.blogspot.com.au/2009/04/final-words-on-tail-calls.html).\" If you do like to play around with recursion more efficiently, I highly recommend taking a look at [Haskell](https://www.haskell.org) or other functional programming languages. That being said, below are some examples of recursive function implementations in Python for illustrative purposes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Factorial" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def factorial(x):\n", " if x <= 1:\n", " return x\n", " else:\n", " return x * factorial(x-1)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factorial(0)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factorial(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5! = 5 x 4 x 3 x 2 x 1 = 120" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factorial(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Length of an array" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def array_len(x):\n", " if x == []:\n", " return 0\n", " else:\n", " return 1 + array_len(x[1:])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_len([])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_len([1, 2, 3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sum of the elements in an array" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def array_sum(x):\n", " if x == []:\n", " return 0\n", " else:\n", " return x[0] + array_sum(x[1:])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_sum([])" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_sum([5])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_sum([1, 2, 3, 4, 5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Binary search using recursion" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def binary_search(array, value, min_idx=0, max_idx=None):\n", " \n", " if min_idx == max_idx:\n", " return None\n", " elif max_idx is None:\n", " max_idx = len(array)\n", " else:\n", " pass\n", "\n", " middle_idx = min_idx + (max_idx - min_idx) // 2\n", "\n", " if array[middle_idx] == value:\n", " return middle_idx\n", " elif array[middle_idx] < value:\n", " min_idx = middle_idx + 1\n", " else:\n", " max_idx = middle_idx\n", " \n", " return binary_search(array, value, min_idx, max_idx)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", " value=1)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", " value=2)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", " value=4)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", " value=11)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", " value=99)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quicksort" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def quicksort(array):\n", " if len(array) < 2:\n", " return array\n", " else:\n", " pivot = array[0]\n", " smaller, bigger = [], []\n", " for ele in array[1:]:\n", " if ele <= pivot:\n", " smaller.append(ele)\n", " else:\n", " bigger.append(ele)\n", " return quicksort(smaller) + [pivot] + quicksort(bigger)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quicksort([])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quicksort([5])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 5]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quicksort([5, 4])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 4, 5, 7]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quicksort([1, 2, 7, 5, 4])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 4, 5]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quicksort([5, 4, 3, 2])" ] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }