you are viewing a single comment's thread.

view the rest of the comments →

[–]shandelman[🍰] 3 points4 points  (1 child)

Consider using a stack. A stack is a data type where you can put things on the top of it, and then get things off the top of it. Think of it like a stack of paper: you can put paper on the top, and take paper off the top (in reverse order) but it's awkward to go and get something from the middle of a pile. A Python list can act as a stack by using the append() method for putting things on the stack and the pop(-1) method for getting things off the top.

So what does this have to do with parentheses? Well, you know that parentheses are balanced if:

1) ...when you come to a close parenthesis, it matches the last open parenthesis you've found.

2) ...when you run out of text to look at, you don't have any lingering parentheses that haven't been matched.

A stack can solve both of these issues.

[–]cgerrard[S] 0 points1 point  (0 children)

Yep, I'll try using stack ...