-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparentesis.py
31 lines (26 loc) · 954 Bytes
/
parentesis.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
# Recibes una string conteniendo solo () y []
# Crea un algoritmos para validar que la sintaxis de los corchetes es correcta:
# Ejemplos validos:
# '()'
# '()()()()()()()()()'
# '([])[]()'
# Ejemplos no validos:
# ')'
# '()['
# '(([))]'
# '(((((((((((((((((((((((((((((((((('
VALID_PARENTHESES_STRING = '()()()()()()()()()'
INVALID_PARENTHESES_STRING = '())](())[]'
OPENING_CHARS, CLOSING_CHARS = '([', ')]'
def parentheses(parentheses_string):
if not parentheses_string:
return True
for i, character in enumerate(parentheses_string):
if character in CLOSING_CHARS:
index = CLOSING_CHARS.index(character)
if parentheses_string[i-1] != OPENING_CHARS[index]:
return False
return parentheses(parentheses_string[:i-1] + parentheses_string[i+1:])
return False
parentheses_string = input("Escribe un string de paréntesis y/o llaves: ")
print(parentheses(parentheses_string))