1
+ #08 CLASES
2
+ """
3
+ /*
4
+ * EJERCICIO:
5
+ * Explora el concepto de clase y crea un ejemplo que implemente un inicializador,
6
+ * atributos y una función que los imprima (teniendo en cuenta las posibilidades
7
+ * de tu lenguaje).
8
+ * Una vez implementada, créala, establece sus parámetros, modifícalos e imprímelos
9
+ * utilizando su función.
10
+ *
11
+ * DIFICULTAD EXTRA (opcional):
12
+ * Implementa dos clases que representen las estructuras de Pila y Cola (estudiadas
13
+ * en el ejercicio número 7 de la ruta de estudio)
14
+ * - Deben poder inicializarse y disponer de operaciones para añadir, eliminar,
15
+ * retornar el número de elementos e imprimir todo su contenido.
16
+ */
17
+ """
18
+
19
+
20
+ class Iphone :
21
+ def __init__ (self ) :
22
+ self .nombre = "iPhone15"
23
+ self .camara = "12MP"
24
+ self .almacenamiento = "265GB"
25
+ self .ram = "8GB"
26
+
27
+ def presentacion (self ):
28
+ return f"""
29
+ Nombre: { self .nombre }
30
+ Camara: { self .camara }
31
+ Almacenamiento: { self .almacenamiento }
32
+ Ram: { self .ram }
33
+ """
34
+
35
+ class Telefono :
36
+ def __init__ (self , nombre :str , camara : str , almacenamiento : str , ram : str ) -> str :
37
+ self .nombre = nombre
38
+ self .camara = camara
39
+ self .almacenamiento = almacenamiento
40
+ self .ram = ram
41
+
42
+ def presentacion (self ):
43
+ return f"""
44
+ Nombre: { self .nombre }
45
+ Camara: { self .camara }
46
+ Almacenamiento: { self .almacenamiento }
47
+ Ram: { self .ram }
48
+ """
49
+
50
+ iphone15 = Iphone ()
51
+ print (iphone15 .presentacion ())
52
+
53
+ Sansung_S23 = Telefono ("Sansung S23" , "200MP" ,"256GB" , "8GB" )
54
+ print (Sansung_S23 .presentacion ())
55
+
56
+ ### Extra
57
+ class Pila :
58
+ def __init__ (self ):
59
+ self .pila : list = []
60
+
61
+ def añadir (self , elemento_a_añadir ):
62
+ self .pila .append (elemento_a_añadir )
63
+
64
+ def eliminar (self ):
65
+ del self .pila [- 1 ]
66
+
67
+ def ultimo_elemento (self ):
68
+ print (self .pila [- 1 ])
69
+
70
+ def pila_actual (self ):
71
+ print (self .pila )
72
+
73
+
74
+ class Cola :
75
+ def __init__ (self ) :
76
+ self .cola : list = []
77
+
78
+ def añadir (self , elemento_a_añadir ):
79
+ self .cola .append (elemento_a_añadir )
80
+ print ("se añadio el elemento" )
81
+
82
+ def eliminar (self ):
83
+ del self .cola [0 ]
84
+ print ("se elimino el elemento" )
85
+
86
+ def primer_elemento (self ):
87
+ print (self .cola [0 ])
88
+
89
+ def cola_actual (self ):
90
+ print (self .cola )
91
+
92
+ cola = Cola ()
93
+ pila = Pila ()
94
+
95
+ while True :
96
+ pila_cola = int (input ("que vas a usar? , colas(1) , pilas(2) o salir(3) : " ))
97
+
98
+ if pila_cola == 1 :
99
+
100
+ while True :
101
+ print ("""
102
+ 1.añadir
103
+ 2.eliminar
104
+ 3.primer_elemento
105
+ 4.cola_actual
106
+ 5.salir
107
+ """ )
108
+ try :
109
+ decicion = int (input ("eliga segun el indice : " ))
110
+ except Exception as error :
111
+ print (" ha habido un error que es : " , error )
112
+
113
+
114
+ if decicion == 1 :
115
+ elemento = input ("elemento a añadir : " )
116
+ cola .añadir (elemento )
117
+
118
+ elif decicion == 2 :
119
+ cola .eliminar ()
120
+
121
+ elif decicion == 3 :
122
+ cola .primer_elemento ()
123
+
124
+ elif decicion == 4 :
125
+ cola .cola_actual ()
126
+
127
+ elif decicion == 5 :
128
+ break
129
+
130
+ else :
131
+ print ("opcion no existente" )
132
+
133
+
134
+ elif pila_cola == 2 :
135
+ while True :
136
+ print ("""
137
+ 1.añadir
138
+ 2.eliminar
139
+ 3.ultimo_elemento
140
+ 4.pila_actual
141
+ 5.salir
142
+ """ )
143
+ try :
144
+ decicion = int (input ("eliga segun el indice : " ))
145
+ except Exception as error :
146
+ print ("ha habido un error el cual es : " , error )
147
+
148
+
149
+ if decicion == 1 :
150
+ elemento = input ("elemento a añadir : " )
151
+ pila .añadir (elemento )
152
+
153
+ elif decicion == 2 :
154
+ pila .eliminar ()
155
+
156
+ elif decicion == 3 :
157
+ pila .ultimo_elemento ()
158
+
159
+ elif decicion == 4 :
160
+ pila .pila_actual ()
161
+
162
+ elif decicion == 5 :
163
+ break
164
+
165
+ else :
166
+ print ("opcion no existente" )
167
+
168
+ elif pila_cola == 3 :
169
+ break
170
+
171
+ else :
172
+ print ("opcion no correcta" )
0 commit comments