algo Flashcards

1
Q

python: To declare a variable without assigning it a value, type

A

my_variable = None

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

algo: For a linked list, the main 2 attributes you need are

A

data and next

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

python: To make the most basic class, type

A
class MyClass:
    pass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

python: Every file that you create is a

A

library

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

python: To import one specific class from an external file, type

A

from library_name import ClassName

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

python: To turn two lists into a dictionary, type

A

dict(zip(list1, list2))

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

python: To slice the last 8 characters from a string, type

A

my_string[-8:]

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

python: To round a number to the nearest whole number, type

A

round(1.3)

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

python: To make a one line if else statement, type

A

“true value” if 10==10 else “false_value”

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

python: To make a node of a linked list that takes in its data upon instantiation, type

A
class Node:
    data = None
    next = None
    def \_\_init\_\_(self, data):
      self.data = data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

python: To call a class method on the class itself, type

A

ClassName.method_name(class_instance)

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

python: To import a class called MyClass from a file called my_file.py in the current directory, type

A

from my_file import MyClass

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

python: All class methods must have a

A

self argument

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

python: To call a class variable from inside a class method, type

A

self.variable_name

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

python: To call a class method from inside another class method, type

A

self.method_name()

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

python: To set a default value for a function, type

A
def my_function(param1, param2 = True):
    return param1

note: Params with default value must all come at the end

17
Q

python: To pass in unlimited named parameters into a function, type

A
def my_function(**kwargs):
    return ...

my_function(param1 = “string”, param2 = “string”):

Note: All the params will be accessible in a dict called kwargs.
Note: The **kwargs arguments must come at the end.

18
Q

python: To create a class where the __init__ function sets all the keyword arguments as attributes, type

A

class MyClass:
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)

19
Q

algo: Usually, your algorithm with use a

A

while True loop instead of a for loop, will use break, pass for logic inside. Afterwards try to replace while True: with like while pointer > 4:

20
Q

python: Attributes in a class that are declared as attribute_name = “string” but not explicitly set using self.attribute_name in the __init__ method or any other method are

A

universal and all the instances can at once have their attribute values changed using MyClass.attribute_name = “string”

21
Q

python: To inherit a class, type

A
class MyClass(ParentClass):
    pass
22
Q

algo: For efficiency, avoid

A

nested loops

23
Q

algo: The purpose of big 0 notation is

A

to assess how much time an algorithm will take to run and how much space it will take in memory, as the input gets arbitrarily larger

24
Q

algo: The main big 0 notations are

A
0(n) = Linear - one loop
0(1) = Static - no loops
0(n^2) = Quadratic - One nested loop
25
Q

algo: For big 0 notation, people usually want to know

A

the worst case scenario

26
Q

algo: In general, the the most efficient

A

functions are the built in ones

27
Q

algo: Hash tables and lists run in

A

constant time

28
Q

python: The two ways to create a generator are

A

(item*item for item in my_list)

or

def my_generator(my_list):
    for item in my_list:
        yield item*item

The generator become and iterable which you can then use a for loop on, or return where the pointer currrently is by running next(my_generator) and the pointer will move to the next index.

29
Q

python: To call a lambda function, type

A

my_function = lambda param1 : param1 *param1

my_function(2)

Note: Lambda functions are mostly used to run a one line function as a parameter into another function to save space.

30
Q

python: If you are not sure if a parameter is the right type, the pythonic approach is

A

duck typing. You put the code in a try/except block and assume the parameter is the correct type

try:
except Exception as e:
raise e

31
Q

python: A double linked list is

A

a list of nodes that are connected forwards using a next attribute and a backwards using the prev attribute