我在Python3中实现deque的运行时错误

时间:2017-11-22 13:46:19

标签: deque

你能帮我找一个deque的实现错误吗?对deque中可能元素的数量有限制(即100)。

我认为我的代码有错误,因为当我试图在竞赛中传递它时,它会因运行时错误而耗尽。
问题在于我寻求的时候 手动发现这个错误我不能得到这样的错误 这是我的代码:

def push_front(dequeue, elem):
    global dequeuestart
    dequeue[dequeuestart] = elem
    dequeuestart -= 1
    print('ok')

def push_back(dequeue, elem):
    global dequeueend
    dequeue[dequeueend] = elem
    dequeueend += 1
    print('ok')

def pop_front(dequeue):
    global dequeuestart
    dequeuestart += 1
    return dequeue[dequeuestart]

def pop_back(dequeue):
    global dequeueend
    dequeueend -= 1
    return dequeue[dequeueend]

def front(dequeue):
    return dequeue[dequeuestart + 1]

def back(dequeue):
    return dequeue[dequeueend - 1]

def size(dequeue):
    return dequeueend - dequeuestart - 1

def clear(dequeue):
    global dequeuestart
    global dequeueend
    dequeuestart = 99
    dequeueend = 100
    print('ok')

def exit(dequeue):
    print('bye')


dequeue = ['None' for i in range(200)]
dequeuestart = 99
dequeueend = 100
line = input()
while line:
    elems = line.split()
    if elems[0] == 'push_front':
        push_front(dequeue, elems[1])
    elif elems[0] == 'push_back':
        push_back(dequeue, int(elems[1]))
    elif elems[0] == 'pop_front':
        print(pop_front(dequeue))
    elif elems[0] == 'pop_back':
        print(pop_back(dequeue))
    elif elems[0] == 'front':
        print(front(dequeue))
    elif elems[0] == 'back':
        print(back(dequeue))
    elif elems[0] == 'size':
        print(size(dequeue))
    elif elems[0] == 'clear':
        clear(dequeue)
    else:
        exit(dequeue)
        break
    line = input()

提前致谢!

0 个答案:

没有答案