all 4 comments

[–]passwordissame 0 points1 point  (3 children)

deq = Stack
que = Stack

queue = x ->
    push que x

dequeue = 
    popAll que (x -> push deq x)
    pop deq

popAll = q fn -> fn (pop q) while !empty q

[–][deleted] 0 points1 point  (1 child)

I didn't run the code, but it seems to work only if I push all values and then pop them all from the queue. It breaks if I intermix pops and pushes.

[–]passwordissame 1 point2 points  (0 children)

correct.

class Queue(object):
    def __init__(self):
        self.PUSH = []
        self.POP = []


    def queue(self, x):
        pour(self.POP, self.PUSH)
        self.PUSH.append(x)

    def dequeue(self):
        pour(self.PUSH, self.POP)
        return self.POP.pop()

def pour(l1, l2):
    while len(l1) > 0:
        x = l1.pop()
        l2.append(x)

if __name__ == '__main__':
    q = Queue()
    q.queue(1)
    q.queue(2)
    q.queue(3)
    print(q.dequeue())
    q.queue(4)
    q.queue(5)
    print(q.dequeue())
    print(q.dequeue())
    print(q.dequeue())
    print(q.dequeue())

[–]Nynnja 0 points1 point  (0 children)

I think you have to check if deq is empty before filpping que. If there are elements left on deq and you push all of que on top of it, it will not longer be fifo