-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpract1.py
executable file
·143 lines (112 loc) · 3.7 KB
/
pract1.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
135
136
137
138
139
140
141
142
143
#Cadenas ej1
def inserta(str, ch):
return ch.join(s for s in str)
def reemplaza_espacio(str, ch):
return str.replace(" ",ch)
def reemplaza_num(str, ch):
return "".join(["X" if i.isdigit() else i for i in str])
def inserta_cada_tres(str, ch):
return ch.join([str[i:i+3] for i in range(0, len(str), 3)])
#Cadenas ej2
def subcadena(str1, str2):
return (str2 in str1)
def orden_alfabetico(str1, str2):
if str1[0] < str2[0]:
return str1
return str2
print inserta("hola", ",")
print reemplaza_espacio("Hola Mundo", "_")
print reemplaza_num("2as34", "X")
print inserta_cada_tres("lskjdlfk", "?")
print subcadena("Acceso Inteligente a la Informacion", "gente")
print orden_alfabetico("alfabeto","betaalfo")
#Tuplas ej1
def campania1(tupla):
for i in tupla:
if i[1] == "H":
print "Estimado " + i[0] + ", vote por mi."
elif i[1] == "M":
print "Estimada " + i[0] + ", vote por mi."
def campania2(tupla, org, cant):
if org > len(tupla) -1 or org + cant > len(tupla):
print "Tamano de la tupla excedido"
else:
filt = tupla[org:org + cant]
campania1(filt)
#Tuplas ej2
def listado(lista):
return [i[1] + " " + i[2] + ". " + i[0] for i in lista]
campania1((("Jose", "H"), ("Alvaro", "H"), ("Manu", "H"), ("Ana", "M")))
campania2((("Jose", "H"), ("Alvaro", "H"), ("Manu", "H"), ("Ana", "M")), 2, 2)
print listado([("Boza", "Francisco", "J"), ("Rodriguez", "Laura", "R")])
#Busqueda
def busqueda(cad, lst):
return [i for i in lst if cad in i[0]]
print busqueda("Romero", [("Maria Romero", "654111227"), ("Jesus Romero", "613550784"), ("Manuel Garcia", "621304877")])
#Diccionarios
def ingresa_nombres():
agenda = {}
while True:
entrada = raw_input("Introduce un nombre: \n")
if entrada == "*":
print ("Gracias")
break
elif entrada not in agenda:
tlf = raw_input("Introduce un telefono: \n")
if tlf == "*":
print ("Gracias")
break
agenda[entrada] = tlf
return agenda
print ingresa_nombres()
#Objetos
class Corcho:
def __init__(self, bodega):
self.bodega = bodega
class Botella:
def __init__(self, corcho = None):
self.corcho = corcho
class Sacacorchos:
def __init__(self):
self.corcho = None
def destapar(self, botella):
self.corcho = botella.corcho
botella.corcho = None
def limpiar(self):
self.corcho = None
corcho = Corcho("La Bodega")
botella = Botella(corcho)
sacacorchos = Sacacorchos()
sacacorchos.destapar(botella)
sacacorchos.limpiar()
#Herencia y polimorfismo
class Personaje:
def __init__(self, vida, posicion, velocidad):
self.vida = vida
self.posicion = posicion
self.velocidad = velocidad
def recibir_ataque(self, cantidad):
self.vida = self.vida - cantidad
if self.vida <= 0:
print "El personaje ha muerto"
def mover(self, direccion, velocidad):
if direccion == "izquierda":
self.posicion = self.posicion - velocidad
elif direccion == "derecha":
self.posicion = self.posicion + velocidad
class Soldado(Personaje):
def __init__(self, vida, posicion, velocidad, ataque):
self.vida = vida
self.posicion = posicion
self.velocidad = velocidad
self.ataque = ataque
def atacar(self, personaje):
personaje.vida = personaje.vida - self.ataque
class Campesino(Personaje):
def __init__(self, vida, posicion, velocidad, cosecha):
self.vida = vida
self.posicion = posicion
self.velocidad = velocidad
self.cosecha = cosecha
def cosechar(self):
return self.cosecha