Recursion and Classes Flashcards

1
Q

What is a recursive function

A

A function that calls itself

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

How to rewrite while to recursive

A
  1. Make each local variable a parameter for the function (set default value)
  2. Replace while with if
  3. Return value from recursive call with updated parameters
  4. In else, return answer based on parameter values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you make a variable with an empty list a parameter (x = [])?

A

x: list|None =None

if x == None:
x = []

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

How can you identify a magic method?

A

format: __name__

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

what is the first parameter for any function in a class

A

self

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

Documentation for a class

A

Write a docstring

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

Annotation for a class

A

List each attribute and annotate type
For each function annotate all parameters except self

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

What is the purpose of a class

A

To store complex data

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

__repr__ purpose

A

Choose what calling print on function will show

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

__init__ purpose

A

Allows you to call attributes of a class

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

__init__ annotation

A

def __init__(self, x: type, y: type) -> None:

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

__repr__ annotation

A

def __repr__(self) -> str:

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

__eq__ purpose

A

Determine if 2 classes have the same attributes

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

__eq__ annotation

A

def __eq__(self, other: any) -> bool:

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

What is a non-magic method

A

Methods that are not pre-defined

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

What is a magic method

A

Method that is built in

17
Q

What type of method requires documentation

A

Non-magic methods