以字母顺序合并两个队列

时间:2018-10-02 16:56:16

标签: python queue

我正在尝试将两个队列(这两个队列假定已按字母顺序排序)合并到第三个队列中,并按字母顺序返回第三个队列。例如:

declare @processowner table(processid int, Owner int);
insert into @processowner values(1,1);
insert into @processowner values(1,2);
insert into @processowner values(1,3);

declare @updated table(processid int);


UPDATE @processowner SET [Owner]=1 
    OUTPUT inserted.[ProcessId] 
    into @updated
WHERE [Owner]=2;

select * From @processowner;

这是我的代码(我不使用方法queue1 = ["a","d","x"] queue2 = ["b","c","y","z"] # the third queue should look like queue3 = ["a","b","c","d","x","y","z"] 进行操作):

merge()

基本上,我的第一个类import string from pythonds.basic.stack import Stack class Stack1Queue: Stackone = Stack() Stackto = Stack() def __init__(self): self.items = [] def add(self, element): self.items.append(element) def remove(self): if len(Stackto) == 0: if len(Stackone) == 0: return None while not Stackone.isEmpty(): p = Stackone.pop() Stackto.push(p) return Stackto.pop() def peek(self): return self.items[len(self.items) - 1] def isEmpty(self): return self.items == [] def size(self): return len(self.items) class MergeQueue(Stack1Queue): adict = dict(enumerate(string.ascii_lowercase)) Queuethr = Stack() def merge(self, Stackone, Stackto): while not Stackone.isEmpty() and Stackto.isEmpty(): for k, v in adict.items(): if Stackone.peek() == adict.get(v): Queuethr.push(Stackone.peek()) else: Queuethr.push(Stackto.peek()) print(dict(Queuethr)) return 1 是实现两个堆栈来创建队列的。无论如何,我只是想知道我的Stack1Queue函数是否正确编写。当我尝试运行时:

merge

对于s = Stack1Queue() p = Stack1Queue() q = MergeQueue() s.add("z") s.add("y") s.add("d") s.add("b") s.add("a") p.add("x") p.add("d") p.add("c") print(q.merge(s, p)) 的大小,它返回0。

1 个答案:

答案 0 :(得分:2)

连接并使用sorted

queue1 = ["a","d","x"]
queue2 = ["b","c","y","z"]

queue3 = sorted(queue1 + queue2)
['a', 'b', 'c', 'd', 'x', 'y', 'z']