Skip to content

Commit 146e3e0

Browse files
committed
#22 - Python
1 parent 623dbf0 commit 146e3e0

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
Crea una función que dibuje una espiral como la del ejemplo.
3+
- Únicamente se indica de forma dinámica el tamaño del lado.
4+
- Símbolos permitidos: ═ ║ ╗ ╔ ╝ ╚
5+
6+
Ejemplo espiral de lado 5 (5 filas y 5 columnas):
7+
════╗
8+
╔══╗║
9+
║╔╗║║
10+
║╚═╝║
11+
╚═══╝
12+
"""
13+
14+
def draw_spiral(size: int) -> None:
15+
"""
16+
Dibuja una espiral cerrada de tamaño N x N usando caracteres de dibujo de cajas permitidos.
17+
18+
La espiral comienza en la esquina superior izquierda y se mueve continuamente hacia adentro.
19+
20+
Args:
21+
size (int): La longitud del lado (número de filas y columnas) de la espiral cuadrada.
22+
Debe ser un entero positivo.
23+
24+
Raises:
25+
ValueError: Si 'size' no es un entero positivo.
26+
"""
27+
if not isinstance(size, int) or size < 1:
28+
raise ValueError("El tamaño debe ser un entero positivo.")
29+
30+
spiral = [[' ' for _ in range(size)] for _ in range(size)]
31+
top, bottom, left, right = 0, size - 1, 0, size - 1
32+
33+
while True:
34+
if left > right or top > bottom:
35+
break
36+
37+
for i in range(left, right + 1):
38+
spiral[top][i] = '═'
39+
if top < bottom:
40+
spiral[top][right] = '╗'
41+
top += 1
42+
if top > bottom:
43+
break
44+
45+
for i in range(top, bottom + 1):
46+
spiral[i][right] = '║'
47+
if left < right:
48+
spiral[bottom][right] = '╝'
49+
right -= 1
50+
if left > right:
51+
break
52+
53+
for i in range(right, left - 1, -1):
54+
spiral[bottom][i] = '═'
55+
if top <= bottom:
56+
spiral[bottom][left] = '╚'
57+
bottom -= 1
58+
if top > bottom:
59+
break
60+
61+
for i in range(bottom, top - 1, -1):
62+
spiral[i][left] = '║'
63+
if left <= right:
64+
spiral[top][left] = '╔'
65+
left += 1
66+
67+
for row in spiral:
68+
print(''.join(row))
69+
70+
71+
if __name__ == "__main__":
72+
draw_spiral(8)

0 commit comments

Comments
 (0)