Python Recursion Tutorial

What is Recursion?

Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem until a base condition is met. It breaks a problem into smaller, simpler versions of itself.

रिकर्शन एक प्रोग्रामिंग तकनीक है जिसमें एक function खुद को कॉल करता है ताकि समस्या के छोटे हिस्सों को हल किया जा सके जब तक कि कोई बेस कंडीशन पूरी न हो जाए। यह समस्या को छोटे, आसान हिस्सों में तोड़ता है।

Why Use Recursion?

Recursion helps to solve problems that can be broken down into similar subproblems. It is useful for tasks like traversing data structures (trees, graphs), factorial calculations, Fibonacci sequences, and more. It often provides clearer and shorter code than iterative solutions.

रिकर्शन उन समस्याओं को हल करने में मदद करता है जिन्हें समान उप-समस्याओं में विभाजित किया जा सकता है। यह पेड़, ग्राफ traversal, फैक्टोरियल, फिबोनैचि श्रृंखला जैसी समस्याओं के लिए उपयोगी है। यह अक्सर पुनरावृत्त समाधानों से साफ़ और संक्षिप्त कोड प्रदान करता है।

Components of Recursion

  • Base Case: Condition where recursion stops to avoid infinite calls.
  • Recursive Case: The part where the function calls itself with modified arguments.
  • बेस केस: वह स्थिति जहाँ रिकर्शन रुक जाता है ताकि अनंत पुनरावृत्ति न हो।
  • रिकर्सिव केस: वह भाग जहाँ function अपने आप को बदले हुए arguments के साथ कॉल करता है।

5 Examples of Recursion with Explanation

Example 1: Factorial Calculation
def factorial(n):
    if n == 0:
        return 1  # Base case
    else:
        return n * factorial(n - 1)  # Recursive call

print(factorial(5))

This function calculates factorial of n. It stops when n is 0 (base case) and otherwise multiplies n by factorial of (n-1).

यह function n का फैक्टोरियल निकालता है। यह तब रुकता है जब n 0 होता है (बेस केस), अन्यथा n को factorial(n-1) से गुणा करता है।

Output:

120
Example 2: Fibonacci Sequence
def fibonacci(n):
    if n <= 1:
        return n  # Base cases
    else:
        return fibonacci(n-1) + fibonacci(n-2)  # Recursive calls

for i in range(6):
    print(fibonacci(i), end=' ')

Computes Fibonacci numbers by adding the two previous Fibonacci numbers. Stops at n=0 or 1.

फिबोनैचि नंबर निकालता है जो पिछले दो नंबरों का योग है। n=0 या 1 पर रुकता है।

Output:

0 1 1 2 3 5
Example 3: Sum of a List
def sum_list(lst):
    if not lst:
        return 0  # Base case: empty list
    else:
        return lst[0] + sum_list(lst[1:])  # Recursive call

print(sum_list([1, 2, 3, 4, 5]))

Calculates sum of list elements by adding first element to sum of rest of list recursively.

सूची के तत्वों का योग निकालता है, पहले तत्व को बाकी सूची के योग से जोड़ता है।

Output:

15
Example 4: Reverse a String
def reverse_string(s):
    if s == "":
        return ""  # Base case
    else:
        return reverse_string(s[1:]) + s[0]  # Recursive call

print(reverse_string('hello'))

Reverses a string by moving first character to end after recursively reversing rest of the string.

एक स्ट्रिंग को उलट देता है, पहले अक्षर को अंत में रखकर बाकी स्ट्रिंग को पुनः उलटता है।

Output:

olleh
Example 5: Nested Folder Size Sum
def folder_size(files):
    total = 0
    for f in files:
        if isinstance(f, list):
            total += folder_size(f)  # Recursive call for subfolder
        else:
            total += f  # File size
    return total

print(folder_size([10, [20, 30], 40]))

Calculates total size of files and subfolders recursively by checking if element is a list (subfolder) or a file size.

फ़ोल्डर और सबफ़ोल्डर के फ़ाइल आकार का योग करता है, recursively subfolder को चेक करता है।

Output:

100