Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e65da45

Browse files
committed
NOTE ON RECURSION
1 parent 2c4b43a commit e65da45

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

Recursive_function.ipynb

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "Recursive_function.ipynb",
7+
"provenance": [],
8+
"authorship_tag": "ABX9TyM5vPLJlRd3thNkO3/BkinC",
9+
"include_colab_link": true
10+
},
11+
"kernelspec": {
12+
"name": "python3",
13+
"display_name": "Python 3"
14+
},
15+
"language_info": {
16+
"name": "python"
17+
}
18+
},
19+
"cells": [
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {
23+
"id": "view-in-github",
24+
"colab_type": "text"
25+
},
26+
"source": [
27+
"<a href=\"https://colab.research.google.com/github/coder9051/Tips-and-tricks/blob/main/Recursive_function.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"source": [
33+
" **NOTE ON RECURSION**\n",
34+
"Recursive function:\n",
35+
"\n",
36+
"A recursive function calls itself. This mean that a new function is called from within the original function.\n",
37+
"\n",
38+
"\n",
39+
"For example look at the code below and assume a linked list counting from 0 to n by 1.\n"
40+
],
41+
"metadata": {
42+
"id": "b6Q_RILdTh_v"
43+
}
44+
},
45+
{
46+
"cell_type": "code",
47+
"source": [
48+
"def print_list(head):\n",
49+
" if head is not None:\n",
50+
" print(head.data)\n",
51+
" print_list(head.next)"
52+
],
53+
"metadata": {
54+
"id": "k9CYsraPUWmn"
55+
},
56+
"execution_count": null,
57+
"outputs": []
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"source": [
62+
"The first function looks at the head of the list and prints it. It then creates a new function to print the next element. However,\n",
63+
"first function hasn't ended yet. It can't end until all the functions within it end. This is key to understanding.\n",
64+
"\n",
65+
"If we look at the call stack (the list of running functions) it looks like this:"
66+
],
67+
"metadata": {
68+
"id": "ogRmJFUOUg0Q"
69+
}
70+
},
71+
{
72+
"cell_type": "code",
73+
"source": [
74+
"print_list() # the original outer most function, waiting for internal functions to end.\n",
75+
" print_list() # the new function called within the original func."
76+
],
77+
"metadata": {
78+
"id": "eFoPtNS2Umkl"
79+
},
80+
"execution_count": null,
81+
"outputs": []
82+
},
83+
{
84+
"cell_type": "markdown",
85+
"source": [
86+
"This just keeps on going. The new function calls another function. Now our stack looks like this:"
87+
],
88+
"metadata": {
89+
"id": "uqeJ2HR4UsYG"
90+
}
91+
},
92+
{
93+
"cell_type": "code",
94+
"source": [
95+
"print_list() # the original function\n",
96+
" print_list() # level 1 function.\n",
97+
" \tprint_list() # level 2 function."
98+
],
99+
"metadata": {
100+
"id": "UVJkNiz7Uw1h"
101+
},
102+
"execution_count": null,
103+
"outputs": []
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"source": [
108+
"The call stack keeps getting bigger and bigger because none of the outer functions can terminate because they are waiting on inner\n",
109+
"functions."
110+
],
111+
"metadata": {
112+
"id": "Cbn1bcI-U6Tw"
113+
}
114+
},
115+
{
116+
"cell_type": "code",
117+
"source": [
118+
"print_list() # the original function\n",
119+
" print_list() # level 1 function.\n",
120+
" \tprint_list() # level 2 function.\n",
121+
" ...\n",
122+
" ...\n",
123+
" ...\n",
124+
" print_list() # level 1000 - Recursion depth hit. Program ends and error displayed."
125+
],
126+
"metadata": {
127+
"id": "9Na1zr-sU-uE"
128+
},
129+
"execution_count": null,
130+
"outputs": []
131+
},
132+
{
133+
"cell_type": "markdown",
134+
"source": [
135+
"The call stack keeps getting bigger and bigger because none of the outer functions can terminate because they are waiting on inner\n",
136+
"functions.\n",
137+
"\n",
138+
"Recursion depth is a count of how many layers are active inside your recursive function. The default recursion depth limit for \n",
139+
"Python 3 is 1000. The limit exists to prevent a stack overflow caused by too much (possibly infinite) recursion.\n",
140+
"\n",
141+
"Recursion is a cool tool and you should use it (or at least understand it). However, some languages have optimized tail call \n",
142+
"recursion (what is the kind of recursion being shown above) and avoid this problem by ending the outer function when the inner one is called. Python has not.\n",
143+
"\n",
144+
"If you are writing a function that may hit the recursion limit it may be better to approach it iteratively (at least in Python)."
145+
],
146+
"metadata": {
147+
"id": "Y6dxnLcSWCBm"
148+
}
149+
}
150+
]
151+
}

0 commit comments

Comments
 (0)