猫狗队列
思路
用两个队列分别保存猫、狗,用各个的入队计数判断任一出队时选择哪一种,指定类别出队则直接从相应队列出队。
实现Python
输入测试testCatDogQ.txt
65
add dog 29
add cat 9
add dog 40
add dog 38
add cat 32
add dog 20
add cat 45
pollAll
add cat 37
isDogEmpty
add cat 23
isCatEmpty
add dog 1
pollCat
pollDog
add cat 22
add dog 39
pollCat
add cat 31
add cat 2
add dog 12
add cat 35
add dog 30
add dog 27
add dog 14
add cat 18
isDogEmpty
add cat 3
add dog 21
add dog 16
isCatEmpty
add cat 13
isDogEmpty
add dog 46
add dog 44
isCatEmpty
add dog 10
isDogEmpty
isEmpty
add dog 42
isDogEmpty
pollAll
isCatEmpty
add dog 4
add dog 6
add dog 15
isDogEmpty
pollAll
isEmpty
add cat 26
add dog 17
add cat 5
add dog 41
add cat 7
add dog 28
add dog 8
add dog 11
add dog 24
add dog 34
isEmpty
add cat 25
add dog 19
add dog 33
add dog 36
add cat 43
#!/usr/bin/python3
class Pet:
def __init__(self, type, id=0, t=0):
self.type = type
self.id = id
self.t = t
def __str__(self) -> str:
return "{} {}".format(self.type, self.id)
class PetQueue:
def __init__(self):
self.qcat = []
self.qdog = []
self.cnt = 0
self.num = 0
def add(self, type: str, id):
self.cnt += 1
self.num += 1
if type == "cat":
self.qcat.append(Pet(type, id, self.cnt))
elif type == "dog":
self.qdog.append(Pet(type, id, self.cnt))
else:
raise ValueError("Pet Type Unknown")
# 不分类别地出队1只
def pollAny(self):
if len(self.qcat) > 0 and len(self.qdog) > 0:
tc = self.qcat[0].t
td = self.qdog[0].t
if tc < td:
a = self.qcat.pop(0)
elif td < tc:
a = self.qdog.pop(0)
else:
raise ValueError("Time Error")
elif len(self.qdog) > 0:
a = self.qdog.pop(0)
elif len(self.qcat) > 0:
a = self.qcat.pop(0)
else:
raise IndexError("Empty Queue Polled")
print(a)
def pollAll(self):
while self.num > 0:
self.pollAny()
self.num -= 1
def pollDog(self):
while len(self.qdog) > 0:
print(self.qdog.pop(0))
self.num -= 1
def pollCat(self):
while len(self.qcat) > 0:
print(self.qcat.pop(0))
self.num -= 1
def isCatEmpty(self):
if len(self.qcat) > 0:
print("no")
else:
print("yes")
def isDogEmpty(self):
if len(self.qdog) > 0:
print("no")
else:
print("yes")
def isEmpty(self):
if len(self.qcat) == 0 and len(self.qdog) == 0:
print("yes")
else:
print("no")
def test(self, fn):
with open(fn, "r") as fp:
n = int(fp.readline())
for _ in range(n):
l = fp.readline().strip("\n").split(" ")
if l[0] == "add":
self.add(l[1], int(l[2]))
elif l[0] == "pollAll":
self.pollAll()
elif l[0] == "pollDog":
self.pollDog()
elif l[0] == "pollCat":
self.pollCat()
elif l[0] == "isDogEmpty":
self.isDogEmpty()
elif l[0] == "isCatEmpty":
self.isCatEmpty()
elif l[0] == "isEmpty":
self.isEmpty()
def run(self):
n = int(input())
for _ in range(n):
l = input().split(" ")
if l[0] == "add":
self.add(l[1], int(l[2]))
elif l[0] == "pollAll":
self.pollAll()
elif l[0] == "pollDog":
self.pollDog()
elif l[0] == "pollCat":
self.pollCat()
elif l[0] == "isDogEmpty":
self.isDogEmpty()
elif l[0] == "isCatEmpty":
self.isCatEmpty()
elif l[0] == "isEmpty":
self.isEmpty()
if __name__ == "__main__":
a = PetQueue()
a.test("./testCatDogQ.txt")
标签:elif,OJ,队列,self,dog,cat,add,def
From: https://blog.csdn.net/CSUzhangpeike/article/details/136688537