forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouredev.py
89 lines (58 loc) · 1.7 KB
/
mouredev.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
"""
Ejercicio
"""
from abc import ABC, abstractmethod
class Form:
def draw(self):
pass
class Square(Form):
def draw(self):
print("Dibuja un cuadrado")
class Circle(Form):
def draw(self):
print("Dibuja un círculo")
class Triangle(Form):
def draw(self):
print("Dibuja un triángulo")
"""
Extra
"""
class Operation(ABC):
@abstractmethod
def execute(self, a, b):
pass
class Addition(Operation):
def execute(self, a, b):
return a + b
class Substration(Operation):
def execute(self, a, b):
return a - b
class Multiplication(Operation):
def execute(self, a, b):
return a * b
class Division(Operation):
def execute(self, a, b):
return a / b
class Power(Operation):
def execute(self, a, b):
return a ** b
class Calculator:
def __init__(self) -> None:
self.operations = {}
def add_operation(self, name, operation):
self.operations[name] = operation
def calculate(self, name, a, b):
if name not in self.operations:
raise ValueError(f"La operación {name} no está soportada.")
return self.operations[name].execute(a, b)
calculator = Calculator()
calculator.add_operation("addition", Addition())
calculator.add_operation("substration", Substration())
calculator.add_operation("multiplication", Multiplication())
calculator.add_operation("division", Division())
calculator.add_operation("power", Power())
print(calculator.calculate("addition", 10, 5))
print(calculator.calculate("substration", 10, 5))
print(calculator.calculate("multiplication", 10, 5))
print(calculator.calculate("division", 10, 5))
print(calculator.calculate("power", 10, 5))