Recursive Functions in Python: Solving Problems by SelfReference

Unlock the power of self-reference in your Python code! Dive into our Python recursive functions tutorial and discover how to solve complex problems with elegant, concise solutions. Intrigued by the magic of functions calling themselves? Explore the secrets of recursion and transform the way you think about programming. The journey to mastering Python starts here β let's begin!
- Python Recursive Functions Tutorial: Solving Problems by Self-Reference
- Introduction to Recursive Functions in Python
- Python Recursion Explained: A Step-by-Step Breakdown
- Python Recursive Function Examples: Practical Applications
- Python Recursive Functions vs. Iterative Solutions
- Avoiding Common Pitfalls in Python Recursive Functions
- Conclusion: Mastering Recursion in Python for Effective Problem Solving
-
Frequently Asked Questions
- What exactly are recursive functions in Python?
- Why would I use a recursive function instead of a loop?
- What is a "base case" and why is it so important in a recursive function?
- Can you give an example of a problem that's well-suited for a recursive solution in Python?
- Are there any potential drawbacks or limitations to using recursive functions in Python?
- Final Reflection: The Enduring Power of Self-Reference
- Unlock the Power of Recursion: Code Smarter, Not Harder!
Python Recursive Functions Tutorial: Solving Problems by Self-Reference
Introduction to Recursive Functions in Python
Understanding Recursion: The Core Concept
Recursion, in its essence, is a powerful problem-solving technique where a function calls itself directly or indirectly to solve smaller instances of the same problem. Imagine you have a set of Russian nesting dolls, also known as Matryoshka dolls. Each doll contains a smaller version of itself until you reach the smallest doll that cannot be opened.
Recursion mirrors this concept: a problem is broken down into smaller, self-similar subproblems until a simple base case is reached, which can be solved directly. This approach is fundamental in computer science and particularly elegant in Python, allowing for concise and readable solutions to complex problems.
Think of recursion as a loop, but instead of explicitly iterating through a sequence of operations, it achieves repetition through function calls. This might seem a bit abstract at first, but the power of recursion lies in its ability to elegantly handle problems that can be naturally defined in terms of smaller, self-similar subproblems.
For instance, calculating the factorial of a number, traversing a tree-like data structure, or implementing algorithms like quicksort and mergesort are all tasks where recursion shines. The key to understanding recursion is grasping the idea of self-reference and how breaking down a problem into smaller, identical parts can lead to a solution.
Why Use Recursive Functions in Python? Benefits and Use Cases
Python recursive functions offer several compelling advantages that make them a valuable tool in a programmer's arsenal. Firstly, recursion often leads to more concise and readable code, especially when dealing with problems that exhibit a recursive structure. For example, traversing a directory structure or processing tree-like data is naturally expressed recursively, resulting in code that is often shorter and easier to understand compared to iterative approaches. This clarity stems from the fact that recursive code mirrors the problem's inherent recursive nature, making the logic more transparent.
Furthermore, recursive functions are particularly well-suited for solving problems that can be naturally broken down into smaller, self-similar subproblems. Consider algorithms like tree traversals (pre-order, in-order, post-order), depth-first search (DFS) in graphs, and divide-and-conquer algorithms like mergesort and quicksort. These algorithms are inherently recursive in nature, and implementing them recursively often results in more elegant and straightforward solutions.
In domains like artificial intelligence and compiler design, where tree-like structures and recursive algorithms are prevalent, understanding and utilizing recursive functions becomes crucial for efficient and effective problem-solving. While iteration can often achieve the same results, recursion can sometimes offer a more intuitive and natural approach, leading to code that is both elegant and easier to maintain.
Python Recursion Explained: A Step-by-Step Breakdown
The Anatomy of a Recursive Function: Base Case and Recursive Step
Every well-structured Python recursive function consists of two essential components: the base case and the recursive step. The base case is the condition that stops the recursion. It's the simplest instance of the problem that can be solved directly, without further recursion. Without a properly defined base case, a recursive function would call itself infinitely, leading to a stack overflow error and program crash. Think of it as the smallest Matryoshka doll that cannot be opened further β you stop there.
The recursive step, on the other hand, is where the function calls itself. This call is made with a modified input that moves the problem closer to the base case. In each recursive call, the problem is reduced to a smaller, self-similar subproblem.
This process continues until the base case is reached. Imagine opening a Matryoshka doll β you're reducing the problem to opening a smaller doll, and you repeat this until you find the smallest doll (base case). The combination of a well-defined base case and a recursive step that progressively simplifies the problem is what makes recursion a powerful and effective problem-solving technique in Python.
Defining the Base Case: Preventing Infinite Loops in Recursion
The base case is arguably the most critical part of a recursive function. It acts as the termination condition, preventing the function from calling itself indefinitely and leading to the dreaded infinite recursion, which will result in a Stack Overflow error in Python. A Stack Overflow error occurs when the call stack, which stores information about active function calls, exceeds its allocated memory. In the context of recursion, each recursive call adds a new frame to the call stack. Without a base case to stop these calls, the stack will eventually overflow.
To define a base case effectively, you must identify the simplest possible input for which the problem can be solved directly, without any further recursive calls. This base case should be easily recognizable and should be checked at the beginning of the recursive function. For example, in a recursive function to calculate the factorial of a number n, the base case is when n is 0 (or 1, depending on the definition). The factorial of 0 is defined as 1, and this can be returned directly without any further recursion. Failing to define a correct base case, or defining one that is never reached, is a common mistake when working with recursive functions, highlighting the paramount importance of careful base case design in Python recursion.
Implementing the Recursive Step: Calling the Function Itself
The recursive step is the heart of a recursive function where the function calls itself to solve a smaller instance of the problem. This self-call is crucial for breaking down the original problem into manageable subproblems that eventually lead to the base case. The key to a correct recursive step is ensuring that each recursive call progresses towards the base case. This means that the input to the recursive call must be modified in a way that reduces the problem's complexity and gets closer to satisfying the base case condition.
For instance, consider the factorial example again. To calculate the factorial of n recursively, the recursive step involves calling the factorial function with n-1. This is because the factorial of n (for n > 0) is defined as n multiplied by the factorial of n-1. By calling the function with n-1, we are reducing the problem to calculating the factorial of a smaller number, progressively approaching the base case where n becomes 0. It's essential that the recursive step makes progress towards the base case; otherwise, the recursion will not terminate, resulting in an infinite loop and a Stack Overflow error. Carefully designing the recursive step to ensure this progression is fundamental to writing correct and efficient Python recursive functions.
Python Recursive Function Examples: Practical Applications
Example 1: Calculating Factorial with Recursion in Python
Calculating the factorial of a non-negative integer n, denoted as n!, is a classic example often used to illustrate recursion. The factorial of n is the product of all positive integers less than or equal to n. Mathematically, it's defined as:
n! = n * (n-1) * (n-2) * ... * 1
and 0! = 1.
Here's how you can implement factorial calculation using recursion in Python:
def factorial_recursive(n):
if n == 0: # Base case: factorial of 0 is 1
return 1
else: # Recursive step: n! = n * (n-1)!
return n * factorial_recursive(n-1)
# Example usage:
number = 5
result = factorial_recursive(number)
print(f"The factorial of {number} is {result}") # Output: The factorial of 5 is 120
In this code, the factorial_recursive function first checks for the base case: if n is 0, it returns 1. Otherwise, it proceeds to the recursive step, returning n multiplied by the result of calling factorial_recursive with n-1. This recursive call breaks down the problem into smaller subproblems until it reaches the base case, effectively calculating the factorial. For instance, factorial_recursive(5) will compute 5 * factorial_recursive(4), which in turn computes 4 * factorial_recursive(3), and so on, until factorial_recursive(0) returns 1, and the results are multiplied back up the call stack to produce the final answer.
Example 2: Fibonacci Sequence Generation Using Python Recursion
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Mathematically, the Fibonacci sequence F(n) is defined as:
F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2) for n > 1
Here's a Python recursive function to calculate the nth Fibonacci number:
def fibonacci_recursive(n):
if n <= 1: # Base cases: F(0) = 0, F(1) = 1
return n
else: # Recursive step: F(n) = F(n-1) + F(n-2)
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)
# Example usage:
number = 10
print(f"Fibonacci sequence up to {number} terms:")
for i in range(number):
print(fibonacci_recursive(i), end=" ") # Output: Fibonacci sequence up to 10 terms: 0 1 1 2 3 5 8 13 21 34
In this code, the fibonacci_recursive function has two base cases: if n is 0 or 1, it returns n directly. For n greater than 1, it recursively calls itself twice: once with n-1 and once with n-2, and returns the sum of the results. This directly mirrors the mathematical definition of the Fibonacci sequence. While elegant, it's important to note that this recursive implementation of Fibonacci is computationally inefficient for larger values of n due to redundant calculations. For instance, calculating fibonacci_recursive(5) will involve recalculating fibonacci_recursive(3) and fibonacci_recursive(2) multiple times.
Example 3: Calculating the Sum of a List with Recursion
Recursion can also be effectively used to perform operations on data structures like lists. Let's consider the problem of calculating the sum of all elements in a list of numbers. We can approach this recursively by considering the sum of a list as the sum of the first element and the sum of the rest of the list. The base case would be an empty list, where the sum is 0.
Here's a Python recursive function to calculate the sum of a list:
def sum_list_recursive(data_list):
if not data_list: # Base case: empty list, sum is 0
return 0
else: # Recursive step: sum of list = first element + sum of the rest
return data_list[0] + sum_list_recursive(data_list[1:])
# Example usage:
numbers = [1, 2, 3, 4, 5]
total_sum = sum_list_recursive(numbers)
print(f"The sum of the list is: {total_sum}") # Output: The sum of the list is: 15
In this sum_list_recursive function, the base case is checked first: if the data_list is empty (i.e., not data_list is True), it returns 0. Otherwise, it takes the first element of the list ( data_list[0]) and adds it to the recursive call of sum_list_recursive with the rest of the list ( data_list[1:]). This process continues, effectively summing up each element of the list until the list becomes empty, at which point the base case is reached, and the final sum is returned by accumulating the results back up the call stack.
Example 4: Implementing Binary Search with Recursion
Binary search is an efficient algorithm for finding a target value within a sorted list. It works by repeatedly dividing the search interval in half. If the middle element is the target value, the search is successful. If the target value is less than the middle element, the search continues in the left half of the list. If the target value is greater, the search continues in the right half. This divide-and-conquer approach naturally lends itself to a recursive implementation.
Here's a Python recursive implementation of binary search:
def binary_search_recursive(data_list, target, low, high):
if high >= low:
mid = (high + low) // 2 # Calculate middle index
if data_list[mid] == target: # Base case: target found at middle
return mid
elif data_list[mid] > target: # Recursive step: search left half
return binary_search_recursive(data_list, target, low, mid - 1)
else: # Recursive step: search right half
return binary_search_recursive(data_list, target, mid + 1, high)
else: # Base case: target not found
return -1
# Example usage:
sorted_numbers = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]
target_value = 23
result_index = binary_search_recursive(sorted_numbers, target_value, 0, len(sorted_numbers) - 1)
if result_index != -1:
print(f"Element {target_value} is present at index {result_index}") # Output: Element 23 is present at index 5
else:
print(f"Element {target_value} is not present in the list")
In this binary_search_recursive function, the base case is when the high index becomes less than the low index, indicating that the target is not in the list, or when the middle element data_list[mid] matches the target. The recursive steps involve calling binary_search_recursive again, but with a reduced search interval. If data_list[mid] is greater than the target, the search continues in the left half (from low to mid - 1). If data_list[mid] is less than the target, the search continues in the right half (from mid + 1 to high). This recursive halving of the search space makes binary search very efficient for large sorted lists, achieving a time complexity of O(log n).
Python Recursive Functions vs. Iterative Solutions
Comparing Recursion and Iteration: Efficiency and Readability
Both recursion and iteration are fundamental control flow mechanisms in programming, allowing for the repetition of code. While both can solve the same problems, they differ in their approach, efficiency, and readability. Recursion solves problems by breaking them down into smaller, self-similar subproblems, whereas iteration solves problems by repeatedly executing a block of code until a condition is met.
In terms of readability, recursive solutions can often be more concise and easier to understand, especially for problems that are naturally recursive, such as tree traversals or fractal generation. The code often mirrors the mathematical or logical definition of the problem more closely. However, for some programmers, iterative solutions might be perceived as more straightforward and easier to follow, particularly for simpler problems. Readability can be subjective and often depends on the programmer's familiarity with recursive thinking.
Regarding efficiency, iterative solutions are generally more efficient in terms of both time and space complexity compared to their recursive counterparts, especially in Python. Recursive calls involve function call overhead, including pushing and popping frames onto the call stack. This overhead can be significant, especially for deep recursion.
In contrast, iterative solutions typically use loops, which have less overhead. Furthermore, recursion can lead to stack overflow errors if the recursion depth becomes too large, a limitation not typically encountered with iteration. For example, calculating Fibonacci numbers recursively as shown earlier is highly inefficient due to repeated calculations, while an iterative approach would be much faster and use less memory.
When to Choose Recursion Over Iteration in Python Programming
Choosing between recursion and iteration in Python depends on the specific problem, the desired balance between code readability and efficiency, and potential limitations like recursion depth. Recursion is often a good choice when:
- The problem has a naturally recursive structure: Problems that can be defined in terms of smaller, self-similar subproblems, like tree traversals, divide-and-conquer algorithms (mergesort, quicksort), and fractal generation, are often elegantly solved using recursion. The recursive code directly reflects the problem's structure, leading to more readable and maintainable solutions.
- Code conciseness and readability are prioritized: When code clarity and conciseness are paramount, and performance is not the primary concern, recursion can lead to shorter and more understandable code, especially for complex recursive problems.
- Limited input size: For problems where the input size is relatively small, and deep recursion is not expected, the overhead of recursion might be acceptable, and the benefits of code clarity might outweigh the performance concerns.
Iteration, on the other hand, is generally preferred when:
- Performance and efficiency are critical: When performance is a major concern, and efficiency in terms of both time and space is paramount, iterative solutions are generally more efficient due to lower overhead and avoidance of stack overflow risks.
- Problems can be solved efficiently iteratively: Many problems, even those that can be solved recursively, can also be solved efficiently using iteration. In such cases, iteration is often the more practical choice due to its performance advantages.
- Large input sizes or deep recursion potential: For problems involving large input sizes or where deep recursion might occur, leading to potential stack overflow errors, iteration is a safer and more reliable approach.
In practice, for performance-critical applications or when dealing with very large datasets, iteration is often favored in Python. However, for problems where recursion provides a more natural and readable solution, and performance is not the absolute bottleneck, recursion can be a valuable tool, especially for educational purposes and for demonstrating elegant problem-solving techniques.
Avoiding Common Pitfalls in Python Recursive Functions
Stack Overflow Errors: Understanding Recursion Depth Limits
One of the most common pitfalls when working with Python recursive functions is encountering Stack Overflow errors. These errors occur when a recursive function calls itself too many times without reaching a base case, causing the call stack to exceed its maximum size. Each recursive call adds a new frame to the call stack, which stores information about the function call, including local variables and return addresses. If the recursion depth becomes excessively large, the call stack runs out of memory, resulting in a Stack Overflow error and program termination.
Python, like most programming languages, has a default recursion depth limit to prevent uncontrolled stack growth and potential system crashes. This limit is typically around 1000 recursive calls by default, although it can vary depending on the system and Python implementation. You can check the current recursion limit using sys.getrecursionlimit() and modify it using sys.setrecursionlimit(limit), but increasing it too much can still lead to Stack Overflow errors and might impact performance.
To avoid Stack Overflow errors, it's crucial to ensure that your recursive functions have well-defined base cases that are guaranteed to be reached for all valid inputs. Carefully analyze your recursive logic to ensure that each recursive call progresses towards the base case and that the recursion depth remains within acceptable limits. For problems that might potentially lead to deep recursion, consider using iterative solutions or techniques like tail recursion optimization (although Python doesn't natively optimize tail recursion to the same extent as some other languages).
Debugging Recursive Functions: Tips and Techniques
Debugging recursive functions can be more challenging than debugging iterative code due to the nested nature of function calls. However, with the right techniques, debugging recursion can be effectively managed.
- Print Statements for Tracing: One of the simplest and most effective debugging techniques for recursion is strategically placing
printstatements within the recursive function. Print the function arguments at the beginning of each call and the return value before returning. This allows you to trace the sequence of recursive calls, the values of variables at each step, and how the function progresses towards the base case. For example, in a factorial function, you could print the value ofnat each recursive call. - Use a Debugger: Python debuggers like
pdbor IDE debuggers provide more powerful tools for debugging recursive functions. You can set breakpoints within the recursive function, step through the code line by line, inspect variables at each step, and examine the call stack. Stepping into and out of recursive calls in a debugger can provide a clear understanding of the function's execution flow and help identify issues in the recursive logic or base cases. - Visualize the Recursion Tree: For some recursive problems, especially those involving tree-like structures or divide-and-conquer algorithms, visualizing the recursion tree can be helpful. Drawing a diagram of the recursive calls and their relationships can make it easier to understand the overall recursive process and identify potential errors in the recursive logic.
- Simplify and Test Base Cases First: When debugging, start by focusing on the base cases. Ensure that your base cases are correctly defined and handled. Test the function with inputs that directly trigger the base cases to verify their correctness. Once the base cases are working correctly, gradually test with more complex inputs that involve recursive calls.
- Check for Infinite Recursion: If you encounter a Stack Overflow error, it's a strong indication of infinite recursion, meaning your base case is not being reached or is incorrectly defined. Carefully review your base case conditions and ensure that the recursive calls are indeed progressing towards the base case.
By combining these debugging techniques, you can systematically analyze and debug your Python recursive functions, identify and fix errors, and gain a deeper understanding of their execution behavior.
Optimizing Recursive Functions for Performance
While recursion can lead to elegant and readable code, it can sometimes be less efficient than iterative solutions, particularly in Python. However, there are techniques to optimize recursive functions and improve their performance.
- Memoization: Memoization is a powerful optimization technique that involves storing the results of expensive function calls and reusing these stored results when the same inputs occur again. This is particularly effective for recursive functions that exhibit overlapping subproblems, such as the recursive Fibonacci calculation example. By storing the results of Fibonacci numbers that have already been computed, you can avoid redundant calculations and significantly improve performance. Python's
functools.lru_cachedecorator provides a convenient way to implement memoization. - Tail Recursion Optimization (Limited in Python): Tail recursion is a specific form of recursion where the recursive call is the very last operation in the function. Some programming languages (but not standard Python to a significant extent) can optimize tail recursion by transforming it into iteration, eliminating the function call overhead and preventing stack overflow errors. While Python doesn't perform full tail recursion optimization automatically, you can sometimes restructure recursive functions to be tail-recursive or use techniques like trampolining to achieve similar benefits in certain cases.
- Reduce Redundant Calculations: Carefully analyze your recursive logic to identify and eliminate redundant calculations. In the inefficient recursive Fibonacci example, the same Fibonacci numbers are recalculated multiple times. Memoization addresses this, but sometimes you can restructure the recursion to avoid these redundant computations in the first place.
- Convert to Iteration: In many cases, if performance is critical, the most effective optimization is to convert the recursive function into an iterative solution. Iterative solutions generally have lower overhead and are often more efficient in Python. Analyze whether an iterative approach can solve the problem with comparable clarity and then consider converting for performance gains.
By applying these optimization techniques, especially memoization, you can significantly improve the performance of Python recursive functions and make them more practical for performance-sensitive applications. However, always consider the trade-off between code readability and performance when choosing between recursion and iteration, and optimize only when necessary.
Conclusion: Mastering Recursion in Python for Effective Problem Solving
Key Takeaways: Python Recursive Functions Tutorial Recap
This Python recursive functions tutorial has explored the fundamental concepts, practical applications, and important considerations when working with recursion in Python. Let's recap the key takeaways:
- Recursion Defined: Recursion is a problem-solving technique where a function calls itself to solve smaller instances of the same problem. It's based on the principle of self-reference and breaking down problems into self-similar subproblems.
- Base Case and Recursive Step: Every recursive function must have a base case to stop the recursion and a recursive step to call itself with a modified input, progressing towards the base case. A correctly defined base case is crucial to prevent infinite recursion and Stack Overflow errors.
- Benefits of Recursion: Recursion can lead to concise, readable, and elegant code, especially for problems with inherent recursive structures. It mirrors the problem's logic and can simplify complex algorithms.
- Examples of Recursive Functions: We explored practical examples like factorial calculation, Fibonacci sequence generation, list summation, and binary search, demonstrating the versatility of recursion in problem-solving.
- Recursion vs. Iteration: While recursion can be elegant, iteration is often more efficient in Python due to lower overhead and avoidance of stack overflow risks. Choose between recursion and iteration based on problem nature, readability needs, and performance requirements.
- Avoiding Pitfalls: Be mindful of Stack Overflow errors by ensuring proper base cases and managing recursion depth. Use debugging techniques like print statements and debuggers to trace recursive calls and identify issues.
- Optimization Techniques: Techniques like memoization can significantly improve the performance of recursive functions by avoiding redundant calculations. Consider converting to iteration when performance is paramount.
Mastering recursion is a valuable skill for any Python programmer. While it might seem abstract initially, understanding its core concepts and practicing with examples will empower you to solve a wide range of problems in a more elegant and efficient way. Recursion is not always the most performant solution, but its ability to simplify complex logic and provide concise code makes it an indispensable tool in your Python programming toolkit. Continue practicing with recursive functions and explore more advanced recursive algorithms to solidify your understanding and become proficient in leveraging the power of self-reference in your code.
Frequently Asked Questions
What exactly are recursive functions in Python?
Recursive functions are functions that call themselves within their own definition. They solve problems by breaking them down into smaller, self-similar subproblems.
Why would I use a recursive function instead of a loop?
While loops and recursion can often achieve the same results, recursion can be more elegant and readable for problems with a naturally recursive structure, like traversing tree data structures. However, recursion can be less efficient due to function call overhead.
What is a "base case" and why is it so important in a recursive function?
The base case is the stopping condition for a recursive function. Without a base case, the function will call itself indefinitely, leading to a stack overflow error.
Can you give an example of a problem that's well-suited for a recursive solution in Python?
Calculating the factorial of a number is a classic example. The factorial of n is n * (n-1)!, which can be easily expressed as a recursive function where the base case is n == 0.
Are there any potential drawbacks or limitations to using recursive functions in Python?
Yes, deep recursion can lead to stack overflow errors if the maximum recursion depth is exceeded. Python also has a recursion limit that can be adjusted, but exceeding it can still cause problems.
Final Reflection: The Enduring Power of Self-Reference
The exploration of recursive functions in Python, as we've seen, is far more than just an academic exercise; it's about understanding a fundamental principle of problem-solving that echoes throughout computer science and beyond. The ability to break down complex tasks into smaller, self-similar subproblems remains a cornerstone of elegant and efficient code, and its relevance only grows as we tackle increasingly intricate challenges in the digital age. From AI algorithms to data structure manipulation, the principles of recursion are silently powering much of the technology we rely on daily.
The beauty of recursion lies not just in its technical application, but also in its mirroring of natural processes. Consider the fractal patterns found in nature, the branching of trees, or even the self-referential nature of human thought β all reflect the underlying principle of recursion. To understand recursion, one must first understand recursion.
This seemingly circular statement encapsulates the core concept: a function defining itself in terms of itself, a concept that, once grasped, unlocks a powerful new way of thinking about problem decomposition and solution design.
As you continue your journey in Python and beyond, remember the lessons learned here. Embrace the power of self-reference, not just in code, but in your own approach to challenges. Dare to break down seemingly insurmountable problems into smaller, manageable steps, and trust in the power of iterative refinement. The ability to see the larger picture while focusing on the immediate task is a skill that will serve you well, not just as a programmer, but as a problem-solver in all aspects of life.
Unlock the Power of Recursion: Code Smarter, Not Harder!
Thanks for exploring the world of recursive functions in Python! We hope this article has given you a solid understanding of how self-reference can elegantly solve complex problems. Ready to put your new skills to the test and master recursion?
Here are a few next steps:
- Start coding! Try applying recursive functions to a problem you're currently working on. Experiment, debug, and learn from the process.
- Explore advanced techniques like tail recursion optimization to improve your code's performance.
- Share this article with other Python programmers and challenge them to solve a classic recursive problem, such as the Tower of Hanoi.
Let's see your creativity! Share your code and insights in the comments below. What's the most interesting problem you solved using recursion, and what elegant solution did you devise?
