forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkenysdev.py
250 lines (212 loc) · 7.45 KB
/
kenysdev.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# ╔══════════════════════════════════════╗
# ║ Autor: Kenys Alvarado ║
# ║ GitHub: https://github.com/Kenysdev ║
# ║ 2024 - Python ║
# ╚══════════════════════════════════════╝
# ----------------------------------------
# Pilas(stack - LIFO):
# ----------------------------------------
# - Es una estructura de datos.
# - Sigue el principio LIFO (last in, first out), significa que
# el último elemento añadido, es el primero en ser retirado.
# Implementar:
class stack:
def __init__(self):
self.elements = []
def __len__(self): # total elementos.
return len(self.elements)
def is_empty(self): # es vacia.
return len(self) == 0
def push(self, item): # Agregar elemento.
self.elements.append(item)
def push_batch(self, items): # Agregar multiples.
# use <history.push_batch(["1", "2", "3"])">
self.elements.extend(items)
def pop_get(self): # Eliminar y obtener.
if not self.is_empty():
return self.elements.pop()
else:
return None
def get_top(self): # Obtener sin eliminar.
if not self.is_empty():
return self.elements[-1]
else:
return None
def get_tuple(self): # retornar pila como tupla.
return tuple(reversed(self.elements))
def clear(self): # vaciar pila.
self.elements.clear()
# ____________________________________________
# Ejercicio usando la implementación de pilas:
"""
Simula el mecanismo adelante/atrás de un navegador web.
- Crea un programa en el que puedas navegar a una página o indicarle que te quieres
desplazar adelante o atrás, mostrando en cada caso el nombre de la web.
- Las palabras "adelante", "atrás" desencadenan esta acción, el resto
se interpreta como el nombre de una nueva web.
"""
msg_nav = """
Historial de navegación:
-------------------------------------------------
Usar: "<" para página anterior.
">" para página adelante.
"h" Ver historial completo.
"x" para salir.
Escribir web para agregar:
-------------------------------------------------"""
def nav_history(history, back_history, forward_history):
def web(url):
print(f"({len(back_history)}) <- Atras <-[Actual: {url}]-> Delante -> ({len(forward_history)})")
def back():
if not back_history.is_empty():
forward_history.push(history.get_top()) # Obtener sin eliminar.
history.push(back_history.pop_get()) # Eliminar y obtener.
web(history.get_top())
else:
print("No hay página previa.")
select()
def forward():
if not forward_history.is_empty():
back_history.push(history.get_top())
history.push(forward_history.pop_get())
web(history.get_top())
else:
print("No hay página siguiente.")
select()
def new_web(url):
back_history.push(history.get_top())
forward_history.clear()
history.push(url)
web(history.get_top())
select()
def the_history():
if not history.is_empty():
for item in history.get_tuple():
print(item)
else:
print("Historial vacío.")
select()
def select():
print("____________")
action = input("acción: ")
if len(action) > 0:
match action:
case "<": back()
case ">": forward()
case "x": main()
case "h": the_history()
case _: new_web(action)
else:
print("Eres muy gracioso xD.")
select()
print(msg_nav)
web(history.get_top())
select()
# -----------------------------------------
# Colas (Queue - FIFO):
# -----------------------------------------
# - Estructura de datos que sigue el principio FIFO(First In, First Out)
# - El primer elemento que se inserta es el primero en ser retirado.
# Implementar:
from collections import deque
class queue:
def __init__(self):
self.elements = deque()
def __len__(self): # total elementos.
return len(self.elements)
def is_empty(self): # es vacia.
return len(self) == 0
def enqueue(self, item): # agregar
self.elements.append(item)
def enqueue_all(self, items): # Agregar multiples.
self.elements.extend(items)
def dequeue(self): # Eliminar y devolver
if not self.is_empty():
return self.elements.popleft()
else:
return None
def peek(self): # Obtener sin eliminar.
if not self.is_empty():
return self.elements[-1]
else:
return None
def get_tuple(self): # retornar cola como tupla.
return tuple(self.elements)
def clear(self): # vaciar cola.
self.elements.clear()
# ____________________________________________
# Ejercicio usando la implementación de Colas:
"""
Simula el mecanismo de una impresora compartida.
- recibe documentos y los imprime cuando así se le indica.
- La palabra "imprimir" imprime un elemento de la cola.
- El resto de palabras se interpretan como nombres de documentos.
"""
msg_printer = """
------------------------------------------------
Usar: "p" Imprimir.
"l" Ver documentos pendientes.
"x" para salir.
Escribir nombre de documento para enviar a cola:
------------------------------------------------"""
def queue_printer(doc_queue):
def print_doc():
if not doc_queue.is_empty():
print(f"{doc_queue.dequeue()} -> ha sido impreso.")
print(f"{len(doc_queue)} -> archivos faltantes")
else:
print("No hay archivos.")
select()
def queue_pending():
if not doc_queue.is_empty():
for doc in doc_queue.get_tuple():
print(doc)
else:
print("No hay archivos.")
select()
def send_doc(doc):
doc_queue.enqueue(doc)
print("Archivo agregado a cola de impresión.")
select()
def select():
print("____________")
action = input("acción: ")
if len(action) > 0:
match action:
case "p": print_doc()
case "l": queue_pending()
case "x": main()
case _: send_doc(action)
else:
print("Eres muy gracioso xD.")
select()
print(msg_printer)
select()
#__________________________________________________________
#__________________________________________________________
msg_home = """
----------------------------------
Usar: "1" para simulador_navegador.
"2" para simulador_impresora.
"Otra tecla" para salir.
----------------------------------
"""
def main():
def case_nav():
back_history = stack()
forward_history = stack()
history = stack()
history.push("Inicio")
nav_history(history, back_history, forward_history)
def case_printer():
doc_queue = queue()
doc_queue.enqueue_all(["a.pdf", "b.txt", "c.docx"])
queue_printer(doc_queue)
print(msg_home)
select = input("seleccionar: ")
match select:
case "1": case_nav()
case "2": case_printer()
case _: print("Bye")
if __name__ == "__main__":
main()