Task 19 - processes only half of data

Back to Problem Solutions forum

ukolem_no     2021-02-21 12:17:30

Dear Admin! When I run my python code either on CodeAbbey or locally (command line) it processes onyl half of data (9 out of 18 lines). Could you give me a hint on what is wrong? When I debugged a bit (I printed data in the final print statement) I learned that code gives right value for line 9, but doesn't print data. When I removed all of the code and just printed data it printed all 18 lines. There is room for improvements, but that will come after I get it to work.

n = int(input())
opens =['(', '[', '{', '<']
closes =[')', ']', '}', '>']

for i in range(n):
    breaking = 0
    data_string = input()
    data = [i for i in data_string]
    opened = []

    for character in data[:]:
        if character not in opens and character not in closes:
            data.remove(character)

    if data[0] not in opens or data[-1] not in closes:
        print(0, '')
        break

    for character in data:

        if character in opens:
            opened.append(character)

        elif len(opened) > 0:
            if character == ')':
                if opened[-1] != '(':
                    breaking = 1
                    break
                else:
                    #print('Closed ()')
                    opened.pop()

            elif character == ']':
                if opened[-1] != '[':
                    breaking = 1
                    break
                else:
                    #print('Closed []')
                    opened.pop()

            elif character == '}':
                if opened[-1] != '{':
                    breaking = 1
                    break
                else:
                    #print('Closed {}')
                    opened.pop()

            elif character == '>':
                if opened[-1] != '<':
                    breaking = 1
                    break
                else:
                    #print('Closed <>')
                    opened.pop()

            else:
                pass
        else:
            breaking = 1
            break
    if len(opened) == 0 and breaking != 1:
        print(1, '')
    else:
        print(0, '')
Rodion (admin)     2021-02-21 17:17:28
User avatar

Hi Friend!

That's easy, but allow me to help you learn a hint about debugging. It is always good to find out "minimal" example on which your program fails. So I tried your code - with some inputs it worked correctly, but with others I found it stops prematurely. I removed some lines from input (reducing count in beginning) which seemingly do not affect the result. At last I come to input of just 2 lines, for which your code gives single 0.

Then I started shortening the lines themselves. In some cases program begins working well, in others it gave 0 again. Soon I understood what the problem is and came up with the following simple input which fails:

3
(
[]
)

Try debugging it and most probably you will easily see where is the flaw! Feel free to come for more hints if necessary.

ukolem_no     2021-02-22 15:07:55

Thank you Admin! Now I feel slightly bad for posting such an easy error, but probably thants to that I will remember this for long time. Have a nice day!

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