W18 Flashcards
The most two important computational resources are:
1- Time 2- Space (memory) Also, we can measure: - number of operations performed - order of growth (big O notation)
Terminology for describing performance:
What is best case, worst case and average case?
best case: is the minimum running time overall possible inputs of a given size
worst case: is the maximum running time over all possible inputs of a given size
average case: is the average running time over all possible inputs
Order of growth ( big O notation) describes what?
the running time in relation to input size (usually the worst case scenario)
ways of sorting:
1- bubble sort
2- selection sort
3- merge sort
ways of searching:
1- linear search
2- bisection search
linear search
1- list does not have to be sorted
2- brute force search???
bisection search
1- list must be sorted
2- divide and conquer approach
A template that describes a set of objects with the same behaviours ??
A class
What is Encapsulation?
1- hiding the implementation details
2- enables changes in the implementation without affecting users of a class
An instance(نموذج) of a class?
An object
object reference?
The memory location of the object
How to check if two objects are aliases (point to the same memory location)?
using (is)
if obj1 is obj2:
print(“Aliases”)
How to check if two objects contain same data?
using ( ==)
if obj1 == obj2:
print(“objects contain same data”)
How to check if an object is not empty before using it?
using (is)
if obj is None:
…..
function vs methos
A method is defined as part of class definition so, it always has, at least, one parameter which is (self)
What is (self reference)?
(self) stores the object reference on which the method was invoked
Accessor and Mutator methods:
get
set
1- how to use the instance variable inside its class?
2- how to call a method inside a method in the same class?
using (self)
1- self.instanceVariable
2- def x(self,num):
self.addOne(num)
should instance variable be encapsulated?
Yes
using accessor and mutator methods
static(class) vs instance variables
- both are declared inside a class.
- however, instance variables are declared inside the constructor
- other classes can call the static variable using the class name: class.variable
What does a constructor do?
initialize instance variables of an object (creates an object and its instance variables)
how to define a constructor?
def __init__(self):
All variables are created at the run time?
True or False
True
what is a default argument?
def __init__(self, age, name, color = white):
self. age= age
self. name= name
self. color= color
- if the user did not enter a color, then by default the color is white.
- this can be used in any method