Search code examples
pythonruntime-error

Runtime error when submitting Kattis problem in python: I can guess the data structure


I'm trying for a while now to submit my solution to the following problem on Kattis: I can guess the data structure!. However, I keep getting a runtime error; I cannot think of anywhere it can go wrong since it works with all my input. Here is my solution:

import heapq, sys
def throwin(q,s,h,x, results):
    if results[0]:
        q.append(x)
    if results[1]:
        s.append(x)
    if results[2]:
        heapq.heappush(h, -x)
    return (q,s ,h )
def printstructures(l):
    for j in l:
        if j[0] and j[1] or j[0] and j[2] or j[1] and j[2]:
            print("not sure")
        elif not j[0] and not j[1] and not j[2]:
            print("impossible")
        elif j[0]:
            print("queue")
        elif j[1]:
            print("stack")
        else:
            print("priority queue")
def main():
    results_global = []
    stackops = []
    current = []
    while True:
        try:
            line = input()
            if len(line) == 1:
                if len(current) != 0:
                    stackops.append(current)
                current = []
            else:
                current.append(tuple(map(int, line.split(" "))))
        except EOFError:
            break
    stackops.append(current)
    for op in stackops:
        q,s,h = [],[],[]
        heapq._heapify_max(h)
        results = [True, True, True]
        for i in range(len(op)):
            o, x = op[i]
            if o == 1:
                q,s,h = throwin(q,s,h,x, results)
            else:
                if len(q) == 0 or q[0] != x:
                    results[0] = False
                else:
                    q.pop(0)
                if len(s) == 0 or s[-1] != x:
                    results[1] = False
                else:
                    s.pop()
                if len(h) == 0 or h[0] != -x :
                    results[2] = False
                else:
                    heapq.heappop(h)
            if i == len(op)-1:
                results_global.append(results)
    printstructures(results_global)
if __name__ == "__main__":
    main()

I was wondering if anyone can give me a push in the right direction and point out where my thinking is wrong or if I made a mistake somewhere I overlooked.


Solution

  • I had the same runtime-error problem for this question, I think it has something to do with python input/output EOFError. I couldn't figure the specific error out but I just put a try/except pass over my entire program and kattis accepted the solution.

    import sys
    try:
        def solve(n):
            stack = []
            queue = []
            priority_queue = []
            type_ds = [True, True, True]
            for i in range(n):
                command, element = map(int, input().split())
                if command == 1:
                    if type_ds[0] != False:
                        stack.append(element)
                    if type_ds[1] != False:
                        queue.append(element)
                    if type_ds[2] != False:
                        priority_queue.append(element)
                elif command == 2:
                    if type_ds[0] != False:
                        if len(stack) == 0:
                            return "impossible"
                        if element != stack.pop():
                            type_ds[0] = False
                            stack.clear()
                    if type_ds[1] != False:
                        if len(queue) == 0:
                            return "impossible"
                        if element != queue.pop(0):
                            type_ds[1] = False
                            queue.clear()
                    if type_ds[2] != False:
                        if len(priority_queue) == 0:
                            return "impossible"
                        priority_queue.sort(reverse=True)
                        if element != priority_queue.pop(0):
                            type_ds[2] = False
                            priority_queue.clear()
    
    
            if type_ds.count(True) > 1:
                return "not sure"
            elif type_ds.count(True) == 0:
                return "impossible"
            else:
                if type_ds[0] == True:
                    return "stack"
                elif type_ds[1] == True:
                    return "queue"
                else:
                    return "priority queue"
    
        for line in sys.stdin:
            if line.strip() == "":
                break
            n = int(line.strip())
            print(solve(n))
    except:
        pass