Last active
July 26, 2016 07:43
-
-
Save hvnsweeting/baab0583e6adfdb417fa27d6df2b076b to your computer and use it in GitHub Desktop.
Recusive function explained by hvn@familug.org #PyFML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Recursive function explained by hvn@familug.org" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"2\n", | |
"3\n" | |
] | |
} | |
], | |
"source": [ | |
"def rprint(n):\n", | |
" '''\n", | |
" Prints from 1 to n recursive way\n", | |
" '''\n", | |
" if n == 0:\n", | |
" return\n", | |
" else:\n", | |
" rprint(n-1)\n", | |
" print(n)\n", | |
"rprint(3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Inside function with 10\n", | |
"|_ calling rprint2(9)\n", | |
" Inside function with 9\n", | |
" |_ calling rprint2(8)\n", | |
" Inside function with 8\n", | |
" |_ calling rprint2(7)\n", | |
" Inside function with 7\n", | |
" |_ calling rprint2(6)\n", | |
" Inside function with 6\n", | |
" |_ calling rprint2(5)\n", | |
" Inside function with 5\n", | |
" |_ calling rprint2(4)\n", | |
" Inside function with 4\n", | |
" |_ calling rprint2(3)\n", | |
" Inside function with 3\n", | |
" |_ calling rprint2(2)\n", | |
" Inside function with 2\n", | |
" |_ calling rprint2(1)\n", | |
" Inside function with 1\n", | |
" |_ calling rprint2(0)\n", | |
" Inside function with 0\n", | |
" | printing 1\n", | |
"1\n", | |
" | printing 2\n", | |
"2\n", | |
" | printing 3\n", | |
"3\n", | |
" | printing 4\n", | |
"4\n", | |
" | printing 5\n", | |
"5\n", | |
" | printing 6\n", | |
"6\n", | |
" | printing 7\n", | |
"7\n", | |
" | printing 8\n", | |
"8\n", | |
" | printing 9\n", | |
"9\n", | |
"| printing 10\n", | |
"10\n" | |
] | |
} | |
], | |
"source": [ | |
"INPUT = 10\n", | |
"def rprint2(n):\n", | |
" pad = 3 * ' ' * (INPUT - n)\n", | |
" print('%sInside function with %d' % (pad, n))\n", | |
" if n == 0:\n", | |
" return\n", | |
" else:\n", | |
" print('%s calling rprint2(%d)' % (pad + '|' + '_', n-1))\n", | |
" rprint2(n-1)\n", | |
" print('%s| printing %d' % (pad, n))\n", | |
" print(n)\n", | |
" return\n", | |
"rprint2(INPUT) " | |
] | |
} | |
], | |
"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.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment