python算法实现——打印任务模拟器

概要

最近在学Python版的数算网课,(打印机模拟器)是其中第四章队列数据的例题
(打印任务模拟),在博客上分享下个人的逻辑思路。

来源

数据结构与算法Python版
第四章例题

问题概述

在这里插入图片描述
问题描述的比较清晰,即打印店人很多,为减少现实资源浪费,耽误学生时间,运用算法来模拟两种 打印模式的结果,辅助决策。

个人认为有意思的点在于:
1.很明显是运用 队列 的问题,但是到底往队列里放什么?
2.如何最有效记录“等待时间”?

直接进入代码。

代码实现

import random
from pythonds.basic.queue import Queue

class Printer:
    def __init__(self, ppm):
        self.pagerate = ppm
        self.currentTask = None
        self.timeRemaining = 0
        
    def tick(self):
        if self.currentTask != None:
            self.timeRemaining = self.timeRemaining -1
            if self.timeRemaining <= 0:
                self.currentTask = None
                
    def busy(self):
        if self.currentTask != None:
            return True
        else:
            return False
    
    def startNext(self,newtask):
        self.currentTask = newtask
        self.timeRemaining = newtask.getPages() *60/self.pagerate

class Task:
    def __init__(self,currenttime):
        self.timestamp = currenttime
        self.pages = random.randrange(1,21)
        
    def getPages(self):
        return self.pages
    
    def getStamp(self):
        return self.timestamp
    
    def waitime(self,currenttime):
        return currenttime - self.timestamp

def newTask():
    num = random.randrange(1,181)
    if num == 180:
        return True
    else:
        return False
    
def simulation(time,pagePerMin):
    labprinter = Printer(pagePerMin)
    waitingtimes = []
    printQueue = Queue()
    for currenttime in range(time):
        if newTask():
            printQueue.enqueue(Task(currenttime))
            
        if (not labprinter.busy()) and (not printQueue.isEmpty()):
            nexttask = printQueue.dequeue()
            waitingtimes.append( \
                nexttask.waitime(currenttime))
            labprinter.startNext(nexttask)

        labprinter.tick()

    averageWait=sum(waitingtimes)/len(waitingtimes)
    print("Average Wait %6.2f secs %3d tasks remaining."\
                    %(averageWait,printQueue.size()))
    
for i in range(10):
    simulation(3600,5)

用python实现队列就不讲了。直接说思路

运用抽象化思想,造了两个类(打印机&任务)

打印机中只处理 处于打印任务的时间
tick用于处理正在打印任务的时间,若打印完返回self.currentTask = None
busy 依托于tick,若有任务在处理,则“关起大门”,拒绝任何任务进入。
startNext 将queue首任务进行打印。

任务,我们需要将任务这一类信息放入队列
主要需要“等待时间”
timestamp创建一个时间针,记录开始时间是多少秒。
waitTime依托于timestamp,作差得结果。
其他def均为辅助。

定义了一个模拟器来统合打印机和任务
重要的是这些:

    for currenttime in range(time):
        if newTask():
            printQueue.enqueue(Task(currenttime))
            
        if (not labprinter.busy()) and (not printQueue.isEmpty()):
            nexttask = printQueue.dequeue()
            waitingtimes.append( \
                nexttask.waitime(currenttime))
            labprinter.startNext(nexttask)

        labprinter.tick()

如果有人需要打印,把任务压到队列排队。
判断如果打印机结束打印,且队列有排队任务
结束等待(挪出排队,开始打印),记录时间至list,
如果已经有任务在打印了,打印剩余时间-1s

总结

看看不同模式的结果

ppm = 5
Average Wait  67.67 secs   1 tasks remaining.
Average Wait 215.11 secs   0 tasks remaining.
Average Wait  70.26 secs   1 tasks remaining.
Average Wait  21.42 secs   0 tasks remaining.
Average Wait  88.41 secs   0 tasks remaining.
Average Wait  88.07 secs   0 tasks remaining.
Average Wait  16.00 secs   0 tasks remaining.
Average Wait 179.77 secs   2 tasks remaining.
Average Wait  19.69 secs   0 tasks remaining.
Average Wait 111.00 secs   0 tasks remaining.
ppm = 10
Average Wait   7.79 secs   0 tasks remaining.
Average Wait  15.00 secs   0 tasks remaining.
Average Wait  19.77 secs   0 tasks remaining.
Average Wait  20.62 secs   0 tasks remaining.
Average Wait   4.64 secs   0 tasks remaining.
Average Wait  15.76 secs   0 tasks remaining.
Average Wait  10.95 secs   0 tasks remaining.
Average Wait   4.67 secs   0 tasks remaining.
Average Wait   4.15 secs   0 tasks remaining.
Average Wait  27.41 secs   0 tasks remaining.

明显应该抛弃一点打印质量选择速度。打印等3分钟…简直要人命

精髓在于创建两个类来模拟 排队&执行 过程。算法就是这样,学习时一头雾水,理清逻辑后感觉好简单,人类智慧大成233.

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值