06 Recursion, Generator Flashcards

1
Q

What is a recursive function?

A

It’s a function that calls itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a recursive function need to avoid running into an endless loop?

A

a base case or recursion anchor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a base case or recursive anchor?

A

It’s a state in a recursive function where, after splitting the task into subtask and rest, the rest can be solved immediately.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the general idea of how to “solve” a recursive function?

A

Split the task into smaller subtask and a rest, call itself and again split the task/rest into a smaller subtask and rest. Repeat until the rest can be solved immediately without having to split it first.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens if a recursive function doesn’t have a base case or recursive anchor?

A

It runs into an endless loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

True or False: There can only be one base case per recursive function.

A

False. A recursive function can have multiple base cases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

True or False: There can be multiple recursive calls in a function.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Apart from running into an endless loop, what other error can occur with recursion?

A

Stack overflow, if the recursion depth is too high.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a recursion depth?

A

The amount of times the function calls itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the default recursion depth in Python and how can it be changed?

A

default: 1000
can be changed: sys.setrecursionlimit(x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a Generator Function?

A

It’s a function, that uses the keyword “yield” instead of “return”. It returns a generator iterator object.
After reaching the “yield” keyword the specified value is returned and the execution of the function is paused. Only if the function is called again does it resume again.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the advantage of a generator function over a normal function?

A

It also yields a result when needed instead of processing everything instantly. That saves memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly