Trees: Is This a Binary Search Tree?

  • + 2 comments

    Maybe you could help me see where I'm going wrong -- I tried the same basic idea as yours, but instead of using an empty list to cover the pop instances, I used an try--except to get around it.

    n = input()
    s = set(map(int, raw_input().split())) 
    
    for _ in range(input()):
        x = list(raw_input().split())
        try:
            int(x[1])
        except IndexError:
            eval('s.%s' % x[0])
        else:
            eval('s.%s(%d)' % (x[0], x[1]))
    print sum(s)
    

    This throws a type error for x[1], saying it needs a number and not a string. But it should already be an int!

    If I replace my last eval with "eval('s.%s(int(%r))', ...) it works! (sort of -- I get the wrong sum as the end)