Matching Brackets

Back to General discussions forum

CurioFibo     2022-12-16 03:34:04

Hello everyone, I'm trying to solve problem 19 with object-oriented programming but my program doesn't pass. I'm using this programming paradigm with the intention of using it in data science. Does anyone have any suggestions.

Thank you very much in advance.

zelevin     2022-12-16 18:19:55

Praticar esse estilo de programação é ótimo, mas a Python list já faz tudo o que você precisa. Talvez primeiro resolva com list e depois acrescentar com sua estrutura de pilha?

CurioFibo     2022-12-17 01:26:36

I'm now using the stack data structure using just list for stacking and popping but the code still doesn't pass as the correct answer.

Thanks in advance for your consideration.

zelevin     2022-12-17 03:24:50

First, I can't quite follow the data loading code. Why not use something straightforward, like l2 = open("problem019.csv").readlines()[1:]? In either case, it would be a good idea to print each line when it starts processing, to make sure you load the input data correctly.

Second, consider the two following cases:

)

and

[)

Both of them would be labeled as correct by your checker: the first one because if len(l3) != 0 will bypass the actual check; and the second one because break will exit the main loop, but your stack would be empty. Perhaps it might be a good idea to rewrite the checker as a function that takes in a single line (not a list of those) and returns (not prints) the zero or one respose? This way, you could return out of it as soon as you find out something does not match.

CurioFibo     2022-12-17 18:55:38

Hello everyone, I managed to fix the code to pass the test. The problem was that sometimes the stack is empty and appears in the expression closing brackets. I needed to put one more conditional if to handle the problem of the empty stack with closing brackets opening the expression.

Thanks for the tips given.

Please login and solve 5 problems to be able to post at forum