-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_ControlarCorchetes.py
46 lines (35 loc) · 1.3 KB
/
04_ControlarCorchetes.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
import re
import sys
def analizar_coherencia_corchetes(filename):
apertura_corchete = "["
cierre_corchete = "]"
try:
with open(filename, "r", encoding="utf-8") as file:
lines_without_match = []
for line_number, line in enumerate(file, start=1):
stack = []
for char in line:
symbol = char
if symbol == apertura_corchete:
stack.append(apertura_corchete)
elif symbol == cierre_corchete:
if stack:
stack.pop()
else:
lines_without_match.append(line_number)
break
if stack:
lines_without_match.append(line_number)
except FileNotFoundError:
print("Error al abrir el archivo.")
return []
return lines_without_match
# Ejemplo de uso
if len(sys.argv) != 2:
print("Uso: python analizar_coherencia_corchetes.py <nombre_del_archivo>")
sys.exit(1)
filename = sys.argv[1]
lines_without_match = analizar_coherencia_corchetes(filename)
print("Líneas sin coincidencias de apertura y cierre de corchetes:")
for line_number in lines_without_match:
print(f"Línea {line_number}")