Skip to content

Commit ba5700e

Browse files
committed
#27 - Python
1 parent 1d4ec3e commit ba5700e

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
from abc import ABC, abstractmethod
2+
import math
3+
'''
4+
EJERCICIO
5+
'''
6+
# Incorrecto
7+
8+
class Circle:
9+
10+
def __init__(self, radius):
11+
self.radius = radius
12+
13+
class Rectangle:
14+
15+
def __init__(self, height, width):
16+
self.height = height
17+
self.width = width
18+
19+
class Square:
20+
21+
def __init__(self, side):
22+
self.side = side
23+
24+
class AreaCalculator:
25+
def calculate_area(self, shape):
26+
if isinstance(shape, Circle):
27+
return math.pi * shape.radius ** 2
28+
elif isinstance(shape, Rectangle):
29+
return shape.height * shape.width
30+
31+
# Correcto
32+
33+
class Shape(ABC):
34+
@abstractmethod
35+
def area(self):
36+
pass
37+
38+
class Circle(Shape):
39+
def __init__(self, radius):
40+
self.radius = radius
41+
42+
def area(self):
43+
return math.pi * self.radius ** 2
44+
45+
class Rectangle(Shape):
46+
def __init__(self, height, width):
47+
self.height = height
48+
self.width = width
49+
50+
def area(self):
51+
return self.height * self.width
52+
53+
class Square(Shape):
54+
def __init__(self, side):
55+
self.side = side
56+
57+
def area(self):
58+
return self.side ** 2;
59+
60+
class AreaCalculator:
61+
def calculateArea(self, shape):
62+
return shape.area()
63+
64+
65+
circle = Circle(7)
66+
rectangle = Rectangle(4, 8)
67+
square = Square(13)
68+
69+
calculator = AreaCalculator()
70+
71+
print(f"Área del círculo: {calculator.calculateArea(circle)}")
72+
print(f"Área del rectangulo: {calculator.calculateArea(rectangle)}")
73+
print(f"Área del cuadrado: {calculator.calculateArea(square)}")
74+
75+
76+
'''
77+
EXTRA
78+
'''
79+
80+
class Operation(ABC):
81+
def __init__(self, value_a, value_b):
82+
self.value_a = value_a
83+
self.value_b = value_b
84+
85+
@abstractmethod
86+
def result(self):
87+
pass
88+
89+
class Add(Operation):
90+
def result(self):
91+
return self.value_a + self.value_b
92+
93+
class Subtract(Operation):
94+
def result(self):
95+
return self.value_a - self.value_b
96+
97+
class Multiply(Operation):
98+
def result(self):
99+
return self.value_a * self.value_b
100+
101+
class Divide(Operation):
102+
def result(self):
103+
return self.value_a / self.value_b
104+
105+
class Calculator:
106+
def operation(self, operation: Operation):
107+
return operation.result()
108+
109+
add = Add(5, 8)
110+
sub = Subtract(5, 8)
111+
mult = Multiply(5, 8)
112+
divd = Divide(5, 8)
113+
114+
calculator = Calculator()
115+
116+
print(f"La suma, 5 + 8 es: {calculator.operation(add)}")
117+
print(f"La resta, 5 - 8 es: {calculator.operation(sub)}")
118+
print(f"La multiplicación, 5 * 8 es: {calculator.operation(mult)}")
119+
print(f"La división, 5 / 8 es: {calculator.operation(divd)}")
120+
121+
class Exponentiation(Operation):
122+
def result(self):
123+
return self.value_a ** self.value_b
124+
125+
exp = Exponentiation(5, 8)
126+
print(f"La potencia, 5 ^ 8 es: {calculator.operation(exp)}")

0 commit comments

Comments
 (0)