Recursion
Recursion is a programming technique in which a function calls itself to solve a problem by reducing it into smaller versions of the same problem. தமிழில்: ஒரு பெரிய problem-ஐ அதே வகையிலான சிறிய-small problem-களாக reduce செய்து, ஒரு functi…
Premium — locked
Advanced
26 min
181 cards
25 programs 11 MCQs v3
This is a premium lesson. You can see the shape of every card — unlock to read them.
Unlock
Recursion என்பது Python functions-ல் மிகவும் முக்கியமான concept. Simple……
Premium
This card is part of the premium lesson.
Unlock
Example
இங்கே show() function உள்ளே மீண்டும் show() call ஆகிறது. இதுதான் recursion-ன் basic idea. ஆனா……
Premium
This card is part of the premium lesson.
Unlock
1. Recursive Call 2. Base Case…
Premium
This card is part of the premium lesson.
Unlock
Recursion is a programming technique in which a function calls itself to solve a problem by reducing it into ……
Premium
This card is part of the premium lesson.
Unlock
Suppose நாம்: 5 4 3 2 1 print செய்ய வேண்டும். Normal method: for i in……
Premium
This card is part of the premium lesson.
Unlock
இங்கே function என்ன செய்கிறது? count(5) ↓ count……
Premium
This card is part of the premium lesson.
Unlock
Main concept
Recursion-ஐ ஒரு staircase போல நினைத்துக்கொள்ளுங்கள். count(5) ↓ count(4) ↓ cou……
Premium
This card is part of the premium lesson.
Unlock
Two most important parts
Recursion-ல் இரண்டு parts com……
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
Function எப்போது stop ஆக வேண்……
Premium
This card is part of the premium lesson.
Unlock
Example
இதுதான் stopping condition.…
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
Function தன்னையே மீண்டும் call ச……
Premium
This card is part of the premium lesson.
Unlock
Recursion = Base Case + Self Call Very important: Bas……
Premium
This card is part of the premium lesson.
Unlock
General syntax
function_name(smaller_value) ……
Premium
This card is part of the premium lesson.
Unlock
return function_name(n - 1)…
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
count(5) ↓ Print 5 ↓ count(4) ↓ Print 4 ↓ count(3) ↓ Prin……
Premium
This card is part of the premium lesson.
Unlock
Why base case is needed
இதோ base case இல்லாத recursion: def count(n): print(n) count(n - 1) count(5) ……
Premium
This card is part of the premium lesson.
Unlock
Recursion error
Common message concept: RecursionError: maximum recursion depth ex……
Premium
This card is part of the premium lesson.
Unlock
Very important
Recursion என்பது technically infinite loop அல்ல. ஆனால் stopping condition இல்……
Premium
This card is part of the premium lesson.
Unlock
Call stack
Recursion புரிய Call Stack concept ரொம்ப முக்கியம். Suppose: def test(n): if n == 0: return print(n) test(n -……
Premium
This card is part of the premium lesson.
Unlock
Call and return
Recursion-ல் இரண்டு phases இர……
Premium
This card is part of the premium lesson.
Unlock
1. Going Down / Calling Phase……
Premium
This card is part of the premium lesson.
Unlock
இதுதான் recursion-ன் most imp……
Premium
This card is part of the premium lesson.
Unlock
Example
def show(n): if n == 0: return pr……
Premium
This card is part of the premium lesson.
Unlock
இதுதான் recursion-ன் real bea……
Premium
This card is part of the premium lesson.
Unlock
Why output reverses
Let's understand inch by inch. show(3) Print Before 3 Call show(2) After 3 இன்னும் execute ஆகவில்லை. Wait செய……
Premium
This card is part of the premium lesson.
Unlock
Visual stack
Calling phase: show(3) ↓ show(2) ↓ show(1) ↓ show(0) Returning……
Premium
This card is part of the premium lesson.
Unlock
Most important exam concept
Recursion uses: Stack data structure / Call Stack Reason: F……
Premium
This card is part of the premium lesson.
Unlock
Stack example
Calls: test(3) test(2) test(1) L……
Premium
This card is part of the premium lesson.
Unlock
Factorial using recursion
Recursion-க்கு classic example:……
Premium
This card is part of the premium lesson.
Unlock
Result
Recursive formula: n! = n × (……
Premium
This card is part of the premium lesson.
Unlock
return n * factorial(n - 1)…
Premium
This card is part of the premium lesson.
Unlock
This line: return n * factorial(n - 1) means: factorial(5) = 5 ×……
Premium
This card is part of the premium lesson.
Unlock
Dry Run
Factorial dry run
Very important
Recursion-ல் result immediately கிடைக்காமல் இருக்கலாம். Function calls first stack-ல் build ஆகு……
Premium
This card is part of the premium lesson.
Unlock
Factorial flow
factorial(5) ↓ 5 * factorial(4) ↓ 4 * factorial(3) ↓ 3 * ……
Premium
This card is part of the premium lesson.
Unlock
Sum using recursion
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
Print 1 to n
Recursion பயன்படுத்தி 1 to 5 print செய்யலாம். def p……
Premium
This card is part of the premium lesson.
Unlock
Why ascending
Notice: print_numbers(n - 1) comes before: print(n) So function first dee……
Premium
This card is part of the premium lesson.
Unlock
Print n to 1
def print_numbers(n): if n == 0: ……
Premium
This card is part of the premium lesson.
Unlock
Very important concept
இந்த இரண்டு programs compare செய்யுங்கள். Print before recursive call print(n) function(n - 1) Outp……
Premium
This card is part of the premium lesson.
Unlock
Fibonacci using recursion
Fibonacci sequence: 0, 1, 1, ……
Premium
This card is part of the premium lesson.
Unlock
F(n) = F(n-1) + F(n-2) Base c……
Premium
This card is part of the premium lesson.
Unlock
return fib(n - 1) + fib(n - 2)…
Premium
This card is part of the premium lesson.
Unlock
Why two recursive calls
For: fib(5) Python calculates: fib(4) + f……
Premium
This card is part of the premium lesson.
Unlock
fib(5) / \ fib(4) fib(3) / \ / \ fib(3)……
Premium
This card is part of the premium lesson.
Unlock
Simple recursive Fibonacci is easy to understand.……
Premium
This card is part of the premium lesson.
Unlock
Example
fib(3) multiple times calculate ஆகலாம். ……
Premium
This card is part of the premium lesson.
Unlock
String recursion
Recursion numbers மட்டும் அல்ல. String processing-க்கும் use செய்யலாம். Exampl……
Premium
This card is part of the premium lesson.
Unlock
Dry Run
reverse("ABC") = reverse("BC") + "A"
Direct recursion
ஒரு function தன்னையே direct-ஆ……
Premium
This card is part of the premium lesson.
Unlock
Example
Premium
This card is part of the premium lesson.
Unlock
Indirect recursion
Function A → Function B → மீண்டும் ……
Premium
This card is part of the premium lesson.
Unlock
Example
a(4) Flow: a() ↓ b() ↓ a() ↓ ……
Premium
This card is part of the premium lesson.
Unlock
Tail recursion
Recursive call function-ன் last oper……
Premium
This card is part of the premium lesson.
Unlock
Example
Recursive call: count(n - 1) ……
Premium
This card is part of the premium lesson.
Unlock
Important python point
Some programming languages tail recursion optimization செய்யலாம். ஆனால் ……
Premium
This card is part of the premium lesson.
Unlock
Comparison
Recursion vs iteration
Recursion மற்றும் Loop இரண்டு……
Premium
This card is part of the premium lesson.
Unlock
Example
count(5) Both same output.…
Premium
This card is part of the premium lesson.
Unlock
When recursion is useful
Recursion especially useful when problem itself recursive structure. Examples: Factoria……
Premium
This card is part of the premium lesson.
Unlock
Real time analogy
Folder inside Folder Computer folder: Main Folder │ ├── File ├── Sub Folder │ ├── File │ └── A……
Premium
This card is part of the premium lesson.
Unlock
Tree example
Tree structure: A / \ B C / \ D E Each node-க்கும் child nodes இருக்கலாம். Node……
Premium
This card is part of the premium lesson.
Unlock
Comparison
Base case vs recursive case
Base Case Recursion stop cond……
Premium
This card is part of the premium lesson.
Unlock
Example
Problem smaller செய்து functi……
Premium
This card is part of the premium lesson.
Unlock
Recursive progress
Base case மட்டும் போதாது. ஒவ்வொரு recursive call-லும் base case-க்கு closer ஆக வேண்டும். Correct: count(n - ……
Premium
This card is part of the premium lesson.
Unlock
ஒரு good recursive function-க்கு
1. Base Case இருக்க வேண்டும் 2. Recursive ……
Premium
This card is part of the premium lesson.
Unlock
இதையே முக்கியமான theory point……
Premium
This card is part of the premium lesson.
Unlock
Error 1
No Base Case def test(): test……
Premium
This card is part of the premium lesson.
Unlock
Result
Error 2
Wrong Direction def count(n): if n == 0……
Premium
This card is part of the premium lesson.
Unlock
Error 3
Base Case Never Reached def count(n): if n……
Premium
This card is part of the premium lesson.
Unlock
Error 4
Forgetting return Factorial wrong: def fact(n): if n == ……
Premium
This card is part of the premium lesson.
Unlock
Error 5
Wrong Base Value Factorial: 0! = 1 So correct: if n == 0:……
Premium
This card is part of the premium lesson.
Unlock
Memory usage
Every recursive call normally requires a new stack frame. Each frame stores things l……
Premium
This card is part of the premium lesson.
Unlock
Each active call has its own ……
Premium
This card is part of the premium lesson.
Unlock
Local scope connection
Previous Local Scope concept இங்கே connect ஆகிறது. def fact(n): ஒவ்வொரு recursive call-க்கும் தனி……
Premium
This card is part of the premium lesson.
Unlock
Return value connection
Recursion return values-ஐ chain ஆக pass செய்யலாம். Factorial: fact(0) returns 1 fact(1) receives 1 return……
Premium
This card is part of the premium lesson.
Unlock
Recursion limit
Python has recursion depth protection. Current limit பார்க்க: import sys print(sys.ge……
Premium
This card is part of the premium lesson.
Unlock
Technically recursion limit change செய்ய: sys.setrecursionlimit(...) possible……
Premium
This card is part of the premium lesson.
Unlock
Comparison
Function composition vs recursion
Previous topic-க்கு comparison: Function Composition f(g(x)) ……
Premium
This card is part of the premium lesson.
Unlock
Comparison
Recursion vs nested function
Nested function: def outer(): def inner()……
Premium
This card is part of the premium lesson.
Unlock
Advantages
Recursion advantages: Code short ஆகலாம் Mathematical definitions easy Tree traver……
Premium
This card is part of the premium lesson.
Unlock
Disadvantages
Recursion disadvantages: Extra memory Function-call overhead Debugging some……
Premium
This card is part of the premium lesson.
Unlock
When not to use
Simple: 1 முதல் 100 வரை print இதற்கு recursion தேவைய……
Premium
This card is part of the premium lesson.
Unlock
When to use
Problem naturally breaks into same smaller problem: fact……
Premium
This card is part of the premium lesson.
Unlock
BSR Recursion-க்கு: B → Base Case S → Sel……
Premium
This card is part of the premium lesson.
Unlock
Another memory trick
Recursion: Call yourself, but know when to stop. தம……
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
1. Recursion stop செய்யும் co……
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
1. Function self-call பகுதி?…
Premium
This card is part of the premium lesson.
Unlock
Recursive Case / Recursive Ca……
Premium
This card is part of the premium lesson.
Unlock
1. Recursion uses which memor……
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
1. Base case இல்லையென்றால்?…
Premium
This card is part of the premium lesson.
Unlock
RecursionError ஏற்படலாம்.…
Premium
This card is part of the premium lesson.
Unlock
1. Function calls itself dire……
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
1. Function A calls B, B call……
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
1. Recursive call last operat……
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
1. Python tail-call optimizat……
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
What is recursion? Recursion is a technique where a function calls itself……
Premium
This card is part of the premium lesson.
Unlock
1. Base Case 2. Recursive Case…
Premium
This card is part of the premium lesson.
Unlock
Additionally, each recursive call must make progress toward the base case. Why is a base case necessary? It p……
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
Recursion primarily uses:…
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
What may happen without a bas……
Premium
This card is part of the premium lesson.
Unlock
2 1 1. What is the base case here? ……
Premium
This card is part of the premium lesson.
Unlock
Premium
This card is part of the premium lesson.
Unlock
A function directly calling i……
Premium
This card is part of the premium lesson.
Unlock
Recursive call at the end of ……
Premium
This card is part of the premium lesson.
Unlock
Which is generally more memor……
Premium
This card is part of the premium lesson.
Unlock
In recursion, each function c……
Premium
This card is part of the premium lesson.
Unlock
Practice
Practice 1
Countdown def count(n): if n ……
Premium
This card is part of the premium lesson.
Unlock
Practice
Practice 2
Sum def total(n): if n == 0: ……
Premium
This card is part of the premium lesson.
Unlock
Dry Run
Premium
This card is part of the premium lesson.
Unlock
Practice
Practice 3
Factorial def fact(n): if n == ……
Premium
This card is part of the premium lesson.
Unlock
Practice
Practice 4
Ascending Print def show(n): ……
Premium
This card is part of the premium lesson.
Unlock
Practice
Practice 5
Find the Error def show(n): if n == 0: return show(n + 1)……
Premium
This card is part of the premium lesson.
Unlock
Recursion basic structure: def function(n): if base_case: return function(smaller_problem) Core flow: Problem……
Premium
This card is part of the premium lesson.
Unlock
1. Base Case இருக்க வேண்டும் 2. Recursive C……
Premium
This card is part of the premium lesson.
Unlock
Short description
Recursion என்பது ஒரு function தன்னையே call செய்து ஒரு பெரிய problem-ஐ அதே வகையிலான smaller problems-ஆக reduce……
Premium
This card is part of the premium lesson.
Unlock