0
I’m trying to solve a problem of a course I’m doing but I’m not able to imagine a way to solve.
I need to create a program where the user type any expression that uses parentheses.
My application should analyze if the expression passed is with the parentheses open and closed in the correct order, and I have no idea how to do this analysis, could help me?
algebra = list()
aberto = fechado = 0
expressao = str(input('Digite sua expressão algébrica com parênteses: '))
if expressao == '(':
aberto += 1
elif expressao == ')':
fechado += 1
if aberto == fechado:
algebra.append(expressao)
print(f'A sua expressão {algebra} está correta!')
print(f'Sua expressão está incorreta, verifique seus parênteses {algebra}')
I have already solved this problem in C using Polish reverse notation and a stack. I do not know if it is the best solution, because the idea when I did it was to solve the algebraic expression, but it is relatively simple. If it is just for checking, possibly count the number of open and closed parentheses and compare if it is the same amount.
– Woss
Take a look at this project: https://github.com/hausen/exprtut. The idea is to shunting-Yard Algorithm an infix postfixed notation (reverse polish notation, RPN).
– William John Adam Trindade
@Williamjohnadamtrindade then but what he’s doing in his code is getting an infix expression and creating a pos, what I need is to receive a posfixa and check if the parenthesis opening is correct. I did more or less this, but he is still always giving as valid see the code I will put in the question.
– Igor Pompeo
An expression any or really an algebraic expression? Its text and its title say contradictory information. For example,
(a + b (/c))
is not algebraic, but respects the formula of parentheses.– Jefferson Quesado