|
| 1 | +# operaciones: introduce - introducir recover - recuperar |
| 2 | + |
| 3 | + |
| 4 | +class Stack: |
| 5 | + def __init__(self): |
| 6 | + # Crea una pila vacía |
| 7 | + self.items = [] |
| 8 | + |
| 9 | + def introduce(self, element): |
| 10 | + # Agrega el elemento element a la pila |
| 11 | + self.items.append(element) |
| 12 | + |
| 13 | + def recover(self): |
| 14 | + # Devuelve el elemento tope y lo elimina |
| 15 | + # Si la pila está vacía levanta una excepción |
| 16 | + try: |
| 17 | + return self.items.pop() |
| 18 | + except IndexError: |
| 19 | + raise ValueError("La pila está vacía") |
| 20 | + |
| 21 | + def is_empty(self): |
| 22 | + # Devuelve True si la pila esta vacía |
| 23 | + return self.items == [] |
| 24 | + |
| 25 | + def size(self): |
| 26 | + return len(self.items) |
| 27 | + |
| 28 | + |
| 29 | +class Queue: |
| 30 | + def __init__(self): |
| 31 | + # Crea una cola vacía |
| 32 | + self.items = [] |
| 33 | + |
| 34 | + def introduce(self, element): |
| 35 | + # Agrega el elemento element a la cola |
| 36 | + self.items.append(element) |
| 37 | + |
| 38 | + def recover(self): |
| 39 | + # Devuelve el primer elemento y lo elimina |
| 40 | + # Si la cola está vacía levanta una excepción |
| 41 | + try: |
| 42 | + return self.items.pop(0) |
| 43 | + except IndexError: |
| 44 | + raise ValueError("La cola está vacía") |
| 45 | + |
| 46 | + def is_empty(self): |
| 47 | + # Devuelve True si la cola esta vacía |
| 48 | + return self.items == [] |
| 49 | + |
| 50 | + |
| 51 | +def web_navigation(): |
| 52 | + navigation = Stack() |
| 53 | + |
| 54 | + while True: |
| 55 | + action_of_navigate = input( |
| 56 | + "Escribe la URL o las palabras atras, adelante o salir" |
| 57 | + ) |
| 58 | + |
| 59 | + if action_of_navigate == "adelante": |
| 60 | + pass |
| 61 | + elif action_of_navigate == "atras": |
| 62 | + if not navigation.is_empty(): |
| 63 | + if navigation.size() > 1: |
| 64 | + page = navigation.recover() |
| 65 | + else: |
| 66 | + page = navigation.items[0] |
| 67 | + elif action_of_navigate == "salir": |
| 68 | + break |
| 69 | + else: |
| 70 | + navigation.introduce(action_of_navigate) |
| 71 | + page = navigation.items |
| 72 | + |
| 73 | + if not navigation.is_empty(): |
| 74 | + print(f"Estas en {page}") |
| 75 | + |
| 76 | + |
| 77 | +def shared_printing(): |
| 78 | + printing_queue = Queue() |
| 79 | + |
| 80 | + while True: |
| 81 | + action = input("Añade un documento o escribe imprimir/salir: ") |
| 82 | + |
| 83 | + if action == "salir": |
| 84 | + break |
| 85 | + elif action == "imprimir": |
| 86 | + if not printing_queue.is_empty(): |
| 87 | + print(f"Imprimiendo el documento {printing_queue.recover()}") |
| 88 | + else: |
| 89 | + printing_queue.introduce(action) |
| 90 | + |
| 91 | + print(f"La cola de impresión restante es: {printing_queue.items}") |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + # web_navigation() |
| 96 | + shared_printing() |
0 commit comments