Skip to content

Commit 9f8d670

Browse files
committed
reto #8 - python
1 parent 9f6bd0a commit 9f8d670

File tree

2 files changed

+332
-0
lines changed

2 files changed

+332
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
from collections import deque
2+
from abc import ABC, abstractmethod
3+
from typing import NoReturn, Collection, Optional, Callable
4+
from enum import StrEnum, Enum
5+
6+
7+
class HistoryEmptyException(Exception):
8+
pass
9+
10+
11+
def raise_history_empty_exception(msg: str) -> NoReturn:
12+
raise HistoryEmptyException(msg)
13+
14+
15+
class Structure[T](ABC):
16+
@abstractmethod
17+
def push(self, element: T) -> None:
18+
pass
19+
20+
@abstractmethod
21+
def pop(self) -> T:
22+
pass
23+
24+
@property
25+
@abstractmethod
26+
def elements(self) -> Collection[T]:
27+
pass
28+
29+
def __str__(self) -> str:
30+
return str(self.elements)
31+
32+
@property
33+
def is_empty(self) -> bool:
34+
return len(self.elements) == 0
35+
36+
37+
class Queue[T](Structure[T]):
38+
def __init__(self) -> None:
39+
self.__elements: deque[T] = deque()
40+
41+
def push(self, element: T) -> None:
42+
self.__elements += [element]
43+
44+
def pop(self) -> T:
45+
return self.__elements.popleft()
46+
47+
@property
48+
def elements(self) -> deque[T]:
49+
return self.__elements
50+
51+
52+
class Stack[T](Structure[T]):
53+
def __init__(self) -> None:
54+
self.__elements: list[T] = []
55+
56+
def push(self, element: T) -> None:
57+
self.__elements += [element]
58+
59+
def pop(self) -> T:
60+
return self.elements.pop()
61+
62+
@property
63+
def elements(self) -> list[T]:
64+
return self.__elements
65+
66+
67+
class Printable(ABC):
68+
@abstractmethod
69+
def print(self) -> None:
70+
pass
71+
72+
@property
73+
@abstractmethod
74+
def document_history(self) -> Structure[str]:
75+
pass
76+
77+
78+
class PrinterActions(Enum):
79+
PRINT: int = 1
80+
EXIT: int = 2
81+
SHOW_DOCUMENT_HISTORY: int = 3
82+
83+
84+
class Printer(Printable):
85+
def __init__(self) -> None:
86+
self.__document_history: Queue[str] = Queue[str]()
87+
88+
def print(self) -> None:
89+
if self.document_history.is_empty:
90+
raise_history_empty_exception("No documents to print")
91+
92+
print("Printing...")
93+
print(self.document_history.pop())
94+
95+
@property
96+
def document_history(self) -> Queue[str]:
97+
return self.__document_history
98+
99+
100+
class Navegable(ABC):
101+
@abstractmethod
102+
def go_to_page(self, page: str) -> None:
103+
pass
104+
105+
@abstractmethod
106+
def undo(self) -> None:
107+
pass
108+
109+
@abstractmethod
110+
def redo(self) -> None:
111+
pass
112+
113+
@abstractmethod
114+
def run(self) -> None:
115+
pass
116+
117+
118+
class WebBrowserActions(StrEnum):
119+
EXIT: str = "exit"
120+
UNDO: str = "undo"
121+
REDO: str = "redo"
122+
GO_TO_PAGE: str = "go_to_page"
123+
124+
125+
class WebBrowser(Navegable):
126+
def __init__(self) -> None:
127+
self.page_history: Stack[str] = Stack[str]()
128+
self.current_page: str = "inicio"
129+
self.current_page_index: int = 0
130+
131+
def go_to_page(self, page: str) -> None:
132+
self.page_history.push(self.current_page)
133+
self.current_page = page
134+
self.current_page_index += 1
135+
136+
print(f"Going to {page}")
137+
138+
def undo(self) -> None:
139+
if self.page_history.is_empty:
140+
raise_history_empty_exception(msg="there are not previous pages to go back")
141+
142+
self.current_page = self.page_history.elements.pop()
143+
print(f"Going back to {self.current_page}")
144+
145+
def redo(self) -> None:
146+
if self.page_history.is_empty:
147+
raise_history_empty_exception(msg="there are not next pages to go forward")
148+
149+
def run(self) -> None:
150+
pass
151+
152+
@property
153+
def operations(self) -> dict[str, Callable]:
154+
return {"go_to_page": self.go_to_page, "undo": self.undo, "redo": self.redo}
155+
156+
157+
def execute_printable(printable: Printable) -> None:
158+
printable.print()
159+
160+
161+
def execute_navegable(navegable: Navegable) -> None:
162+
navegable.run()
163+
164+
165+
def main() -> None:
166+
# Generic queue
167+
queue = Queue[int]()
168+
queue.push(1)
169+
queue.push(2)
170+
queue.push(3)
171+
print(queue.elements)
172+
print(queue.pop())
173+
print(queue)
174+
print(queue.is_empty)
175+
176+
# Generic stack
177+
stack = Stack[int]()
178+
stack.push(1)
179+
stack.push(2)
180+
stack.push(3)
181+
print(stack.elements)
182+
print(stack)
183+
print(stack.pop())
184+
185+
print(stack.elements)
186+
print(stack)
187+
print(stack.is_empty)
188+
189+
190+
if __name__ == "__main__":
191+
main()
+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
from abc import ABC, abstractmethod
2+
from collections import deque
3+
from typing import Collection
4+
5+
6+
class Person:
7+
def __init__(self, name: str, surname: str, age: int) -> None:
8+
self.__name = name
9+
self.__surname = surname
10+
self.__age = age
11+
12+
def full_info(self) -> str:
13+
return f"{self.__name} {self.__surname} - {self.__age} years"
14+
15+
@property
16+
def name(self) -> str:
17+
return self.__name
18+
19+
@property
20+
def surname(self) -> str:
21+
return self.__surname
22+
23+
@property
24+
def age(self) -> int:
25+
return self.__age
26+
27+
@name.setter
28+
def name(self, name: str) -> None:
29+
self.__name = name
30+
31+
@age.setter
32+
def age(self, age: int) -> None:
33+
self.__age = age
34+
35+
@surname.setter
36+
def surname(self, surname: str) -> None:
37+
self.__surname = surname
38+
39+
def __str__(self) -> str:
40+
name, surname, age = self.__name, self.__surname, self.__age
41+
return f"Person({name=} {surname=} {age=})"
42+
43+
44+
def create_person(name: str, surname: str, age: int) -> Person:
45+
return Person(name, surname, age)
46+
47+
48+
class Structure[T](ABC):
49+
@abstractmethod
50+
def push(self, element: T) -> None:
51+
pass
52+
53+
@abstractmethod
54+
def pop(self) -> T:
55+
pass
56+
57+
@property
58+
@abstractmethod
59+
def elements(self) -> Collection[T]:
60+
pass
61+
62+
def __str__(self) -> str:
63+
return str(self.elements)
64+
65+
@property
66+
def is_empty(self) -> bool:
67+
return len(self.elements) == 0
68+
69+
@property
70+
def length(self) -> int:
71+
return len(self.elements)
72+
73+
74+
class Queue[T](Structure[T]):
75+
def __init__(self) -> None:
76+
self.__elements: deque[T] = deque()
77+
78+
def push(self, element: T) -> None:
79+
self.__elements += [element]
80+
81+
def pop(self) -> T:
82+
return self.__elements.popleft()
83+
84+
@property
85+
def elements(self) -> deque[T]:
86+
return self.__elements
87+
88+
89+
class Stack[T](Structure[T]):
90+
def __init__(self) -> None:
91+
self.__elements: list[T] = []
92+
93+
def push(self, element: T) -> None:
94+
self.__elements += [element]
95+
96+
def pop(self) -> T:
97+
return self.elements.pop()
98+
99+
@property
100+
def elements(self) -> list[T]:
101+
return self.__elements
102+
103+
104+
def main() -> None:
105+
kevin = create_person(name="kevin", surname="dueñas", age=23)
106+
print(kevin.full_info())
107+
print(kevin)
108+
109+
kevin.surname = "asael"
110+
kevin.age = 24
111+
print(kevin)
112+
print(kevin.full_info())
113+
114+
# Generic queue
115+
queue = Queue[int]()
116+
queue.push(1)
117+
queue.push(2)
118+
queue.push(3)
119+
print(queue.elements)
120+
print(queue.pop())
121+
print(queue)
122+
print(queue.is_empty)
123+
print(queue.length)
124+
125+
# Generic stack
126+
stack = Stack[int]()
127+
stack.push(1)
128+
stack.push(2)
129+
stack.push(3)
130+
print(stack.elements)
131+
print(stack)
132+
print(stack.pop())
133+
134+
print(stack.elements)
135+
print(stack)
136+
print(stack.is_empty)
137+
print(stack.length)
138+
139+
140+
if __name__ == "__main__":
141+
main()

0 commit comments

Comments
 (0)