Dsap l03 PDF
Dsap l03 PDF
Heikki Peura
[email protected]
1 def K(n):
2 """ assume n is a nonnegative integer """
3 y = 0
4 i = n
5 j = 0
6 while i > 0:
7 while j < i:
8 y += y
9 j += 1
10 i = i//2
11 return y
https://goo.gl/qnXpYW
2 / 19
Last time
Algorithm complexity
I Asymptotic analysis and Big-O notation
I Searching and sorting
3 / 19
Data structures
4 / 19
We have already been using data structures
1 'Hello World'
2 3.14159
3 9
4 L = [1, 1999, 0, -2, 9]
5 / 19
We have already been using data structures
1 'Hello World'
2 3.14159
3 9
4 L = [1, 1999, 0, -2, 9]
5 / 19
We have already been using data structures
1 'Hello World'
2 3.14159
3 9
4 L = [1, 1999, 0, -2, 9]
5 / 19
A Python list has many operations
Some of the operations:
len(L),max(L),min(L),...
L[start:stop:step]: returns elements of L from start to stop
with step size step
L[i]= e: sets the value at index i to e
L.append(e): adds e to the end of L
L.count(e): returns how many times e occurs in L
L.insert(i,e): inserts e at index i of L
L.extend(L1): appends the items of L1 to the end of L
L.remove(e): deletes the first occurrence of e from L
L.index(e): returns the index of first occurrence of e in L
L.pop(i): removes and returns the item at index i, default i = -1
L.sort(): sorts elements of L
L.reverse(): reverses the order of elements of L
6 / 19
A list is an object
7 / 19
A list is an object
1 L = [1,1999,0,-2,9]
2 L.append(8)
3 L.insert(2,1000)
4 t = L.pop()
5 L.remove(1)
6 help(L)
7 / 19
A list is an object
1 L = [1,1999,0,-2,9]
2 L.append(8)
3 L.insert(2,1000)
4 t = L.pop()
5 L.remove(1)
6 help(L)
The point:
I Interface: the user knows what she can do with a list
I Abstraction: the user does not need to know the details of what
goes on under the hood (similarly to functions)
I Invaluable in managing complexity of programs
7 / 19
List operations complexity?
1 L = [1,1999,0,-2,9]
2 L.append(9)
3 t = L[2]
4 t = L.pop(0)
8 / 19
List operations complexity?
1 L = [1,1999,0,-2,9]
2 L.append(9)
3 t = L[2]
4 t = L.pop(0)
8 / 19
How can we implement a list?
9 / 19
How can we implement a list?
An indexed array?
56 24 99 32 9 61 57 79
9 / 19
How can we implement a list?
An indexed array?
56 24 99 32 9 61 57 79
9 / 19
How can we implement a list?
An indexed array?
56 24 99 32 9 61 57 79
9 / 19
How can we implement a list?
An indexed array?
56 24 99 32 9 61 57 79
9 / 19
A linked list of “nodes”?
Linked list?
56 24 99 32 9 61 57 79
10 / 19
A linked list of “nodes”?
Linked list?
56 24 99 32 9 61 57 79
10 / 19
A linked list of “nodes”?
Linked list?
56 24 99 32 9 61 57 79
10 / 19
A linked list of “nodes”?
Linked list?
56 24 99 32 9 61 57 79
10 / 19
A Python list has many operations
Some of the operations:
len(L),max(L),min(L),...
L[start:stop:step]: returns elements of L from start to stop
with step size step
L[i]= e: sets the value at index i to e
L.append(e): adds e to the end of L
L.count(e): returns how many times e occurs in L
L.insert(i,e): inserts e at index i of L
L.extend(L1): appends the items of L1 to the end of L
L.remove(e): deletes the first occurrence of e from L
L.index(e): returns the index of first occurrence of e in L
L.pop(i): removes and returns the item at index i, default i = -1
L.sort(): sorts elements of L
L.reverse(): reverses the order of elements of L
11 / 19
Object-oriented programming (OOP)
12 / 19
Object-oriented programming (OOP)
12 / 19
Object-oriented programming (OOP)
12 / 19
Why define objects?
13 / 19
Why define objects?
1 # Using lists?
2 monsters = ['Pikachu','Squirtle','Mew']
3 combat_points = [20,82,194]
4 hit_points = [53,90,289]
13 / 19
Why define objects?
1 # Using lists?
2 monsters = ['Pikachu','Squirtle','Mew']
3 combat_points = [20,82,194]
4 hit_points = [53,90,289]
1 # Using a dictionary?
2 monsters = {'Pikachu':[20,53],'Squirtle':[82,90],'Mew':[194,289]}
13 / 19
Defining an object type
An object contains
I Data: attributes (of a monster)
I Functions: methods that operate on that data
14 / 19
Defining an object type
An object contains
I Data: attributes (of a monster)
I Functions: methods that operate on that data
1 class Monster(object):
2 """
3 Attributes and methods
4 """
14 / 19
We’ve created a Monster
Attributes of a monster?
15 / 19
We’ve created a Monster
Attributes of a monster?
1 class Monster(object):
2 """
3 Pocket monster
4 """
5 def __init__(self,combat_points):
6 self.combat_points = combat_points
7
8 Pikachu = Monster(65)
9 Squirtle = Monster(278)
10 print(Pikachu.combat_points)
15 / 19
We’ve created a Monster
Attributes of a monster?
1 class Monster(object):
2 """
3 Pocket monster
4 """
5 def __init__(self,combat_points):
6 self.combat_points = combat_points
7
8 Pikachu = Monster(65)
9 Squirtle = Monster(278)
10 print(Pikachu.combat_points)
1 class Monster(object):
2 def __init__(self, name, combat_points, hit_points):
3 self.name = name
4 self.combat_points = combat_points
5 self.hit_points = hit_points
6 self.health = hit_points
7
8 def hurt(self, damage):
9 self.health = self.health - damage
10 if self.health <= 0:
11 print(self.name + ' is dead!')
16 / 19
Why OOP is useful
17 / 19
Accessing data
1 class Monster(object):
2 def __init__(self, name, combat_points, hit_points):
3 self.name = name
4 self.combat_points = combat_points
5 self.hit_points = hit_points
6 self.health = hit_points
7
8 def get_combat_points(self): # access data through method
9 return self.combat_points
10
11 # more Monster code...
12
13 Pikachu = Monster('Pikachu', 100, 30)
14 print(Pikachu.combat_points == 100) # risky - what if you make a mistake?
15 print(Pikachu.get_combat_points() == 100) # safer - cannot mess stuff up
(If you go deeper into OOP, there are more advanced ways of making data
“private”)
18 / 19
Review
19 / 19
Workshop