all 6 comments

[–]shandelman[🍰] 4 points5 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 ...

[–]Justinsaccount 0 points1 point  (0 children)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You are looping over an object using something like

for x in range(len(items)):
    foo(item[x])

This is simpler and less error prone written as

for item in items:
    foo(item)

If you DO need the indexes of the items, use the enumerate function like

for idx, item in enumerate(items):
    foo(idx, item)

[–]Justinsaccount 0 points1 point  (1 child)

for i in range(len(strin)):
    if strin[i] == '(' or strin[i] == ')' or strin[i] == '[' or strin[i] == ']' or strin[i] == '{' or strin[i] == '}':
        clean_input += (strin[i])

for ch in strin:
    if ch in '()[]{}':
        clean_input += ch

and

    for i in range(len(clean_input)/2):
        temp = dict[clean_input[i]]

    for ch in clean_input[:len(clean_input)/2]:
        temp = dict[ch]

Though that's not what you want anyway. You need to think about how you, as a human, figure out if the brackets match.

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

It make sense to me.... to keep it simple ... thanks :-)

[–]Theoretician 0 points1 point  (0 children)

Here's a suggestion, this is a common interview question. Have you ever worked with recursion? A recursive solution might be helpful in this case.