1. Composició i information hiding
1.1. Exercici 3.2 - 3.3
1 class Wallet(object):
2 """
3 >>> m1 = Wallet(6, 4, 3)
4 >>> m2 = Wallet(1, 1, 1)
5 >>> m1.value()
6 29
7 >>> m2.value()
8 8
9 >>> m2.add_coins(1, 3)
10 >>> m2.add_coins(2, 4)
11 >>> m2.add_coins(5, 5)
12 >>> print m2
13 wallet: [ 1 => 4 2 => 5 5 => 6 ]
14 >>> m2.value()
15 44
16 >>> m1.te_canvi(6)
17 True
18 >>> print m1
19 wallet: [ 1 => 6 2 => 4 5 => 3 ]
20 >>> m1.canvi(6)
21 (1, 0, 1)
22 >>> print m1
23 wallet: [ 1 => 5 2 => 4 5 => 2 ]
24
25 """
26 def __init__(self, q1=0, q2=0, q5=0):
27 self.mon ={1:q1, 2:q2, 5:q5}
28
29 def value(self):
30 return sum([k*v for k,v in self.mon.items()])
31
32 def add_coins(self, v, q):
33 """
34 Afegeix una quantitat de monedes d'un valor al moneder
35 """
36 self.mon[v] += q
37
38 def add(self,m):
39 """
40 Suma dos moneders
41 """
42 for v in self.mon:
43 self.mon[v] += m.mon[v]
44
45 def __str__(self):
46 s = "wallet: ["
47 for k,v in self.mon.items():
48 if v != 0: s += " %d => %d " %(k,v)
49 return s + "]"
50
51 def te_canvi(self, x):
52 """
53 Retorna si el monede conte suficient canvi per tornar x diners
54 """
55 c = x
56 c5 = 0
57 c2 = 0
58 c1 = 0
59 while c >= 5 and c5 < self.mon[5]:
60 c -= 5
61 c5 += 1
62 while c >= 2 and c2 < self.mon[2]:
63 c -= 2
64 c2 += 1
65 while c >= 1 and c1 < self.mon[1]:
66 c -= 1
67 c1 += 1
68 return c == 0
69
70 def canvi(self, x):
71 """
72 Retorna el nombre de monedes de cada valor que es treuen del moneder
73 per tornar el canvi
74 """
75 c = x
76 n5=0
77 n2=0
78 n1=0
79 if self.te_canvi(c):
80 while c >= 5 and self.mon[5] != 0:
81 self.mon[5] -= 1
82 n5 += 1
83 c -= 5
84 while c >= 2 and self.mon[2] != 0:
85 self.mon[2] -= 1
86 n2 += 1
87 c -= 1
88 while c >= 1 and self.mon[1] != 0:
89 self.mon[1] -= 1
90 n1 += 1
91 c -= 1
92 return (n5, n2, n1)
1.2. Exercici 3.4
Atenció: Mòdul incomplet!!
1 class Cotxe(object):
2 def __init__(self, matric, model, color, tipus):
3 """
4 matrícula(str)
5 model(str)
6 color(str)
7 tipus de motor(bool) dièsel(True) benzina(False)
8 """
9 self.mat=matric
10 self.mod=model
11 self.col=color
12 self._tip=tipus
13
14 class Garatge(object):
15 def __init__(self):
16 self._ll=[]
17
18 def add(self, c):
19 v=0
20 for cot in self._ll:
21 if cot.mat == c.mat:
22 v += 1
23 if v == 0:
24 self._ll.append(c)
25
26 def treure(self, mat):
27 for c in self._ll:
28 if c.mat == mat:
29 return c
30
31 def num_cotxes(self):
32 return len(self._ll)
33
34 Opcio 2:
35
36 from random import randint
37 class Cotxe(object):
38 def __init__(self,mat,mod,col,tip):
39
40 self.mat=mat
41 self.mod=mod
42 self.col=col
43 self.tip=tip
44
45 class Garatge(object):
46 def __init__(self):
47 self._gar=[]
48
49 def afegir(self,c):
50 trobat=True
51 for element in self._gar:
52 if element.mat == c.mat:
53 trobat= False
54 if trobat:
55 self._gar.append(c)
56
57 def treure(self,mat):
58
59 for i,element in enumerate(self._gar):
60 if element.mat== mat:
61 a= self._gar[i]
62 del self._gar[i]
63 return a
64
65 def ocupacio(self):
66
67 return len(self._gar)
68
69 def aleatori(self):
70 a=randint(0,self.ocupacio()-1)
71 b=self._gar[a]
72 del self._gar[a]
73 return b
1.3. exercici 3.6
1 from random import randint
2 class Cotxe(object):
3 def __init__(self,mat,mod,col,tip):
4
5 self.mat=mat
6 self.mod=mod
7 self.col=col
8 self.tip=tip
9
10 def tostring(self):
11 a= str(self.mat)+','+ str(self.mod)+','+str(self.col)+','+str(self.tip)
12 return a
13 def fromstring(self,s):
14 a=s.strip(',')
15 self.__init__(a[0],a[1],a[2],a[3])