猫狗队列问题

问题描述:


 * 猫狗队列 :
 * 用户可以调用add方法将cat类或dog类的 实例放入队列中;
 * 用户可以调用pollAll方法,将队列中所有的实例按照进队列 的先后顺序依次弹出;
 * 用户可以调用pollDog方法,将队列中dog类的实例按照 进队列的先后顺序依次弹出;
 * 用户可以调用pollCat方法,将队列中cat类的实 例按照进队列的先后顺序依次弹出;
 * 用户可以调用isEmpty方法,检查队列中是 否还有dog或cat的实例; 用户可以调用isDogEmpty方法,
 * 检查队列中是否有dog 类的实例; 用户可以调用isCatEmpty方法,检查队列中是否有cat类的实例
 

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#__author__ = 'Kane'
#Editing time : 2019/5/6 19:06
#File Name : 猫狗队列问题.py

class Dog():
    def __init__(self,name):
        self.name = name
        self.type = 'dog'

    def setCount(self,count):
        self.count = count
    def getCount(self):
        return self.count

class Cat():
    def __init__(self,name):
        self.name = name
        self.type = 'cat'

    def setCount(self,count):
        self.count = count
    def getCount(self):
        return self.count


class DogCatQueue():
    def __init__(self):
        self.dogQ = []
        self.catQ = []
        self.count = 0
    def add(self,pet):
        if pet.type == 'dog':
            self.dogQ.append(pet)
            self.count+=1
            pet.setCount(self.count)
        elif pet.type == 'cat':
            self.catQ.append(pet)
            self.count+=1
            pet.setCount(self.count)
        else:
            raise Exception ("It must be a 'cat' or 'dog'")
    def pollAll(self):
        if (not self.is_CatEmpty()) and (not self.is_DogEmpty()):
            if self.dogQ[0].getCount()<self.catQ[0].getCount():
                c = self.dogQ.pop(0)
                return c.name,c.getCount()
            else:
                d = self.catQ.pop(0)
                return d.name, d.getCount()
        elif self.is_DogEmpty():
            d = self.catQ.pop(0)
            return d.name,d.getCount()
        elif self.is_CatEmpty():
            c = self.dagQ.pop(0)
            return c.name,c.getCount()
        else:
            raise Exception ("both dog or cat queue are null!")
    def poolCat(self):
        if self.is_CatEmpty():
            raise Exception("cat queue is null!")
        else:
            return self.catQ.pop(0)
    def poolDog(self):
        if self.is_DogEmpty():
            raise Exception("dog queue is null!")
        else:
            return self.dogQ.pop(0)

    def is_Empty(self):
        if self.is_CatEmpty() and self.is_DogEmpty():
            return True
        else:
            return False
    def is_CatEmpty(self):
        if not self.catQ:
            return True
        else:
            return False
    def is_DogEmpty(self):
        if not self.dogQ:
            return True
        else:
            return False
    def show(self):
        if not self.is_CatEmpty():
            print('dag queue')
            for j in self.dogQ:
                print((j.name,j.getCount()),end="")
                print('-----------')
        if not self.is_DogEmpty():
            print('cat queue')
            for i in self.catQ:
                print((i.name,i.getCount()),end="")
                print('------------')
'/for test/'
if __name__ == "__main__":

    dog1=Dog("dog1")
    cat1=Cat("cat1")
    dog2=Dog("dog2")
    cat2=Cat("cat2")
    dog3=Dog("dog3")
    cat3=Cat("cat3")

    test = DogCatQueue()
    test.add(dog1)
    test.add(cat1)
    test.add(dog2)
    test.add(cat2)
    test.add(dog3)
    test.add(cat3)
    print("入队结果:")
    test.show()
    print('-------------')

    # print("")
    # while not test.is_CatEmpty():
    #     print(test.poolCat())
    # print("")
    # while not test.is_DogEmpty():
    #     print(test.poolDog())



    print("")
    print("出队结果:")
    while not test.is_Empty():
        print(test.pollAll())

参考:https://blog.csdn.net/xujiao1179/article/details/85060714

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值