|
1 | 1 |
|
| 2 | +# Recursion in Python |
| 3 | + |
| 4 | +Recursion is a fundamental concept in computer science and programming where a function calls itself to solve smaller instances of the same problem. This README.md file provides an overview of recursion in Python, including its definition, common use cases, and examples. |
| 5 | + |
| 6 | +## Table of Contents |
| 7 | +- [What is Recursion?](#what-is-recursion) |
| 8 | +- [Base Case and Recursive Case](#base-case-and-recursive-case) |
| 9 | +- [Common Use Cases](#common-use-cases) |
| 10 | +- [Examples](#examples) |
| 11 | + - [Factorial](#factorial) |
| 12 | + - [Fibonacci Sequence](#fibonacci-sequence) |
| 13 | + - [Sum of a List](#sum-of-a-list) |
| 14 | + - [Binary Search](#binary-search) |
| 15 | +- [Advantages and Disadvantages](#advantages-and-disadvantages) |
| 16 | +- [Conclusion](#conclusion) |
| 17 | + |
| 18 | +## What is Recursion? |
| 19 | + |
| 20 | +Recursion occurs when a function calls itself as part of its execution. Each recursive call processes a smaller portion of the problem until a base case (termination condition) is met, which stops the recursion. |
| 21 | + |
| 22 | +## Base Case and Recursive Case |
| 23 | + |
| 24 | +A recursive function typically has two main components: |
| 25 | +1. **Base Case**: The condition under which the recursion ends. This prevents infinite recursion and eventual stack overflow. |
| 26 | +2. **Recursive Case**: The part of the function where the function calls itself with a smaller or simpler argument. |
| 27 | + |
| 28 | +## Common Use Cases |
| 29 | + |
| 30 | +Recursion is often used for problems that can be broken down into smaller, repetitive tasks. Common use cases include: |
| 31 | +- Mathematical computations (e.g., factorials, Fibonacci numbers) |
| 32 | +- Tree and graph traversals |
| 33 | +- Sorting algorithms (e.g., quicksort, mergesort) |
| 34 | +- Divide and conquer algorithms (e.g., binary search) |
| 35 | + |
| 36 | +## Examples |
| 37 | + |
| 38 | +### Factorial |
| 39 | + |
| 40 | +The factorial of a non-negative integer `n` is the product of all positive integers less than or equal to `n`. |
| 41 | + |
| 42 | +```python |
| 43 | +def factorial(n): |
| 44 | + if n == 0: |
| 45 | + return 1 |
| 46 | + else: |
| 47 | + return n * factorial(n - 1) |
| 48 | + |
| 49 | +print(factorial(5)) # Output: 120 |
| 50 | +``` |
| 51 | + |
| 52 | +### Fibonacci Sequence |
| 53 | + |
| 54 | +The Fibonacci sequence is defined as follows: `F(0) = 0`, `F(1) = 1`, and `F(n) = F(n-1) + F(n-2)` for `n > 1`. |
| 55 | + |
| 56 | +```python |
| 57 | +def fibonacci(n): |
| 58 | + if n <= 0: |
| 59 | + return 0 |
| 60 | + elif n == 1: |
| 61 | + return 1 |
| 62 | + else: |
| 63 | + return fibonacci(n - 1) + fibonacci(n - 2) |
| 64 | + |
| 65 | +print(fibonacci(6)) # Output: 8 |
| 66 | +``` |
| 67 | + |
| 68 | +### Sum of a List |
| 69 | + |
| 70 | +Recursively summing the elements of a list. |
| 71 | + |
| 72 | +```python |
| 73 | +def sum_list(lst): |
| 74 | + if not lst: |
| 75 | + return 0 |
| 76 | + else: |
| 77 | + return lst[0] + sum_list(lst[1:]) |
| 78 | + |
| 79 | +print(sum_list([1, 2, 3, 4])) # Output: 10 |
| 80 | +``` |
| 81 | + |
| 82 | +### Binary Search |
| 83 | + |
| 84 | +Binary search is a divide-and-conquer algorithm to find an element in a sorted list. |
| 85 | + |
| 86 | +```python |
| 87 | +def binary_search(arr, target, low, high): |
| 88 | + if low > high: |
| 89 | + return -1 |
| 90 | + mid = (low + high) // 2 |
| 91 | + if arr[mid] == target: |
| 92 | + return mid |
| 93 | + elif arr[mid] > target: |
| 94 | + return binary_search(arr, target, low, mid - 1) |
| 95 | + else: |
| 96 | + return binary_search(arr, target, mid + 1, high) |
| 97 | + |
| 98 | +arr = [1, 2, 3, 4, 5, 6, 7] |
| 99 | +target = 4 |
| 100 | +print(binary_search(arr, target, 0, len(arr) - 1)) # Output: 3 |
| 101 | +``` |
| 102 | + |
| 103 | +## Advantages and Disadvantages |
| 104 | + |
| 105 | +### Advantages |
| 106 | +- Simplifies code for problems that can be divided into similar sub-problems. |
| 107 | +- Often more intuitive and easier to implement for certain algorithms (e.g., tree traversals). |
| 108 | + |
| 109 | +### Disadvantages |
| 110 | +- Can lead to high memory usage and stack overflow if not implemented correctly. |
| 111 | +- Generally, less efficient due to overhead of multiple function calls. |
| 112 | + |
| 113 | +## Conclusion |
| 114 | + |
| 115 | +Recursion is a powerful tool in a programmer's toolkit. Understanding how to properly implement recursive functions in Python can help solve complex problems more elegantly and succinctly. However, it is important to be mindful of the base case to prevent infinite recursion and to consider the efficiency impact on your program. |
0 commit comments