u06_slides-flashcards

1
Q

Front

A

Back

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

What is a recursive function?

A

A function that calls itself, either directly or indirectly (via other function calls)

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

What are the key components of a recursive function?

A
  1. Base case (recursion anchor) that ends the recursion
  2. Recursive case that breaks the problem into smaller subtasks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a base case in recursion?

A

A condition where the function can solve the problem immediately without making additional recursive calls, preventing infinite recursion

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

When should you use recursion instead of iteration?

A

Use recursion when it’s the most natural way to solve a problem, especially for:
1. Traversing tree-like data structures
2. Processing nested data structures
3. Problems using ‘divide and conquer’ principle

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

What are the drawbacks of recursion?

A
  1. Can be less efficient than iteration due to function call overhead
  2. Risk of stack overflow with high recursion depth
  3. Default recursion depth limit in Python is 1000
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you increase Python’s recursion limit?

A

Using sys.setrecursionlimit(…)

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

What is a generator function?

A

A function that uses ‘yield’ instead of ‘return’ to return a generator iterator object, executing code only when elements are requested

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

How is a generator function’s execution different from regular functions?

A

Generator functions suspend execution after reaching a yield statement and resume when the next element is requested, rather than processing everything at once

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

What is the advantage of using generator functions?

A

They generate elements on-demand rather than processing everything at once, which can save memory

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

How do you access elements from a generator?

A
  1. Using a for loop to iterate through elements
  2. Using the built-in next() function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What happens when you call next() on a generator that has no more elements?

A

It raises a StopIteration exception

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

Can generator functions create infinite sequences?

A

Yes, they can create infinite sequences since they only generate values when requested, making them memory-efficient

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

What happens when you create a generator object without requesting elements?

A

The generator function’s code is not executed until elements are actually requested (lazy evaluation)

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

What’s an example of a simple generator function?

A

def generate_str_numbers(n):
for i in range(n):
yield str(i)

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

How does the power function example demonstrate recursion?

A

It breaks down x^y into x * x^(y-1), recursively solving smaller problems until reaching the base case of y=1