forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKevinED11.py
292 lines (207 loc) · 6.64 KB
/
KevinED11.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from collections import deque
from abc import ABC, abstractmethod
from typing import NoReturn, Collection, Callable, Iterable, Self
from enum import StrEnum
from functools import partial
class HistoryEmptyException(Exception):
pass
class ProgramNotRunningException(Exception):
pass
def raise_history_empty_exception(msg: str) -> NoReturn:
raise HistoryEmptyException(msg)
def raise_program_not_running_exception(msg: str) -> NoReturn:
raise ProgramNotRunningException(msg)
raise_program_not_running_partial_fn = partial(
raise_program_not_running_exception, msg="the program is not running"
)
class Structure[T](ABC):
@abstractmethod
def push(self, element: T) -> Self:
pass
@abstractmethod
def pop(self) -> T:
pass
@property
@abstractmethod
def elements(self) -> Collection[T]:
pass
def __str__(self) -> str:
return str(self.elements)
@property
def is_empty(self) -> bool:
return len(self.elements) == 0
def __iter__(self):
return iter(self.elements)
class Queue[T](Structure[T]):
def __init__(self) -> None:
self.__elements: deque[T] = deque()
def push(self, element: T) -> Self:
self.__elements += [element]
return self
def pop(self) -> T:
return self.__elements.popleft()
@property
def elements(self) -> deque[T]:
return self.__elements
class Stack[T](Structure[T]):
def __init__(self) -> None:
self.__elements: list[T] = []
def push(self, element: T) -> Self:
self.__elements += [element]
return self
def pop(self) -> T:
return self.elements.pop()
@property
def elements(self) -> list[T]:
return self.__elements
class Printable(ABC):
@abstractmethod
def print(self) -> Self:
pass
@abstractmethod
def set_document(self, document: str) -> Self:
pass
@property
@abstractmethod
def document_history(self) -> Structure[str]:
pass
def user_input(prompt: str) -> str:
return input(prompt).lower()
class PrinterActions(StrEnum):
PRINT: str = "1"
EXIT: str = "2"
SHOW_DOCUMENT_HISTORY: str = "3"
SET_DOCUMENT: str = "4"
class Printer(Printable):
def __init__(self) -> None:
self.__document_history: Queue[str] = Queue[str]()
def print(self) -> Self:
if self.document_history.is_empty:
raise_history_empty_exception("No documents to print")
print("Printing...")
print(self.document_history.pop())
return self
def set_document(self, document: str) -> Self:
self.document_history.push(document)
return self
@property
def document_history(self) -> Queue[str]:
return self.__document_history
class Navegable(ABC):
@abstractmethod
def go_to_page(self, page: str) -> Self:
pass
@abstractmethod
def undo(self) -> Self:
pass
@abstractmethod
def redo(self) -> Self:
pass
@abstractmethod
def run(self) -> None:
pass
@property
@abstractmethod
def page_history(self) -> Structure[str]:
pass
@property
@abstractmethod
def current_page(self) -> str:
pass
class WebBrowserActions(StrEnum):
EXIT: str = "exit"
UNDO: str = "undo"
REDO: str = "redo"
GO_TO_PAGE: str = "go_to_page"
class WebBrowser(Navegable):
def __init__(self) -> None:
self.__page_history: Stack[str] = Stack[str]()
self.__current_page: str = "inicio"
self.current_page_index: int = 0
self.__running = False
def go_to_page(self, page: str) -> Self:
if not self.__running:
raise_program_not_running_partial_fn()
self.page_history.push(self.current_page)
self.__current_page = page
self.current_page_index += 1
print(f"Going to {page}")
return self
def undo(self) -> Self:
if not self.__running:
raise_program_not_running_partial_fn()
if self.page_history.is_empty:
raise_history_empty_exception(msg="there are not previous pages to go back")
self.__current_page = self.page_history.elements.pop()
print(f"Going back to {self.current_page}")
return self
def redo(self) -> Self:
if not self.__running:
raise_program_not_running_partial_fn()
if self.page_history.is_empty:
raise_history_empty_exception(msg="there are not next pages to go forward")
return self
def run(self) -> None:
self.__running = True
while self.__running:
print(f"Current page: {self.current_page}")
def __operations(self) -> dict[str, Callable]:
return {"go_to_page": self.go_to_page, "undo": self.undo, "redo": self.redo}
@property
def page_history(self) -> Stack[str]:
return self.__page_history
@property
def current_page(self) -> str:
return self.__current_page
def execute_printable(printable: Printable) -> None:
while True:
print("1 - Print, 2 - Exit, 3 - Show document history, 4 - Set document")
user_option = user_input(prompt="Choose an option: ")
match user_option:
case PrinterActions.PRINT:
try:
printable.print()
except HistoryEmptyException as err:
print(err)
case PrinterActions.EXIT:
print("Bye!")
break
case PrinterActions.SHOW_DOCUMENT_HISTORY:
if printable.document_history.is_empty:
print("No documents to print")
continue
for document in printable.document_history:
print(document)
case PrinterActions.SET_DOCUMENT:
document = user_input(prompt="Enter a document: ")
printable.set_document(document=document)
print(f"Document set: {document} successfully")
case _:
print("Invalid option")
def execute_navegable(navegable: Navegable) -> None:
navegable.run()
def main() -> None:
# Generic queue
queue = Queue[int]()
queue.push(1)
queue.push(2)
queue.push(3)
print(queue.elements)
print(queue.pop())
print(queue)
print(queue.is_empty)
# Generic stack
stack = Stack[int]()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.elements)
print(stack)
print(stack.pop())
print(stack.elements)
print(stack)
print(stack.is_empty)
printer = Printer()
execute_printable(printable=printer)
if __name__ == "__main__":
main()