5.1 Thinking Recursively Flashcards
What is recursion?
Recursion is a function to solve itself.
What does recursion need?
A base case.
What is a base case?
Something that causes the loop to stop. So that it doesn’t get locked and use all of system resources.
What is the base case in this code?
if n==1
What is the difference between recursion and iteration??
an iterative function is one that loops to repeat some part of the code, and a recursive function is one that calls itself again to repeat the code.
What are the rules of recursion?
The size of the problem must get smaller
There is a general case
There is a base case
Why must recursion problems get smaller?
So that the loop doesn’t use all system resources.
What is a general case?
the case for which the solution is expressed in terms of a smaller version of itself. In other words elif and else.
What are the positives of recursion?
Less code required to do a task.
Easier to understand and debug.
What are the negatives of recursion?
Recursion always requires more memory.
Danger of stack overflow if base case isn’t there or too high.
Usually slower
How is recursion helpful in a binary array?
Because you are repeating a process.
What is required when using recursion in a binary search?
Discarding half the array when it is not needed, each time to reduce size.
What is recursion in code?
If/else
What is iteration in code?
For loop.
What is another name for iteration?
Nested loop