-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppd_4.py
134 lines (108 loc) · 2.91 KB
/
ppd_4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class Quadrado(object):
"""
>>> quadrado = Quadrado(2)
>>> quadrado.get_lado()
2
>>> quadrado.set_lado(3)
>>> quadrado.get_lado()
3
>>> quadrado.get_area()
9
"""
def __init__(self, lado):
self.lado = lado
def get_lado(self):
return self.lado
def set_lado(self, lado):
self.lado = lado
def get_area(self):
return self.lado ** 2
class Lista(list):
def sem_repeticao(self):
"""
>>> lista = Lista((1, 2, 3, 2))
>>> lista.sem_repeticao()
[1, 2, 3]
"""
#Na marreta vale?
return list(set(self))
def sem_repeticao_nem_marreta(self):
"""
>>> lista = Lista((1, 2, 3, 2))
>>> lista.sem_repeticao_nem_marreta()
[1, 2, 3]
"""
resultado = []
for value in self:
if value not in resultado:
resultado.append(value)
return resultado
class Carro(object):
"""
>>> siena = Carro(10)
>>> siena.combustivel
0
>>> siena.mover(10)
'Sem gasolina? Como?'
>>> siena.gasolina()
0
>>> siena.abastecer(2)
>>> siena.gasolina()
2
>>> siena.mover(10)
>>> siena.gasolina()
1
>>> siena.mover(200)
Traceback (most recent call last):
...
ValueError: precisa de mais gasolina do que tem
"""
def __init__(self, consumo):
self.consumo = consumo
self.combustivel = 0
def gasolina(self):
return self.combustivel
def abastecer(self, litros):
self.combustivel += litros
def mover(self, distancia):
litros_por_km = 1./self.consumo
if self.combustivel == 0:
return 'Sem gasolina? Como?'
elif litros_por_km * distancia > self.combustivel:
raise ValueError("precisa de mais gasolina do que tem")
self.combustivel -= int(litros_por_km * distancia)
class Vetor(object):
"""
>>> v = Vetor(1, 1, 1)
>>> w = Vetor(2, 2, 2)
>>> v + w
<Vetor(3, 3, 3)>
>>> w - v
<Vetor(1, 1, 1)>
>>> v * w
6
>>> v * 2
Traceback (most recent call last):
...
NotImplementedError
"""
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __add__(self, other):
# O z entra no calculo da soma e da subtracao?
return Vetor(self.x + other.x, self.y + other.y, self.z + other.z)
def __sub__(self, other):
# O z entra no calculo da soma e da subtracao?
return Vetor(self.x - other.x, self.y - other.y, self.z - other.z)
def __mul__(self, other):
if isinstance(other, self.__class__):
return self.x * other.x + self.y * other.y + self.z * other.z
else:
raise NotImplementedError
def __repr__(self):
return u'<Vetor(%s, %s, %s)>' % (self.x, self.y, self.z)
if __name__ == '__main__':
import doctest
doctest.testmod()