#!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Data Structures>> # Release 3.0 # chengang882 @ 2016-12-20 # 它可以将常见的中缀表达式转换成后缀表达式,并计算这个表达示的值 # Completed implementation of a queue ADT import random #数据结构 class Queue(object): def __init__(self): self.items = [] def is_empty(self): return self.items == [] def enqueue(self, item): self.items.insert(0, item) def dequeue(self): return self.items.pop() def peek(self): return self.items[len(self.items)-1] def size(self): return len(self.items) def show(self): return self.items #模拟打印机任务的空忙算法 class Printer(object): def __init__(self, ppm): self.page_rate = ppm self.current_task = None self.time_remaining = 0 def tick(self): if self.current_task != None: self.time_remaining -= 1 if self.time_remaining <= 0: self.current_task = None def busy(self): if self.current_task != None: return True else: return False def start_next(self, new_task): self.current_task = new_task self.time_remaining = new_task.get_pages() * 60 / self.page_rate class Task(object): def __init__(self, time): self.timestamp = time self.pages = random.randrange(1, 21) def get_stamp(self): return self.timestamp def get_pages(self): return self.pages def wait_time(self, current_time): return current_time - self.timestamp def simulation(num_seconds, pages_per_minute): lab_printer = Printer(pages_per_minute) print_queue = Queue() waiting_times = [] for current_second in range(num_seconds): if new_print_task(): task = Task(current_second) print_queue.enqueue(task) if (not lab_printer.busy()) and (not print_queue.is_empty()): next_task = print_queue.dequeue() waiting_times.append(next_task.wait_time(current_second)) lab_printer.start_next(next_task) lab_printer.tick() average_wait = sum(waiting_times) / len(waiting_times) print("Average Wait %6.2f secs %3d task remainging." %(average_wait, print_queue.size())) def new_print_task(): num = random.randrange(1, 181) if num == 180: return True else: return False #实现hot_potato游戏的算法 def hot_potato(name_list, num): sim_queue = Queue() for name in name_list: sim_queue.enqueue(name) while sim_queue.size() > 1: for i in range(num): sim_queue.enqueue(sim_queue.dequeue()) sim_queue.dequeue() return sim_queue.dequeue() if __name__ == "__main__": print(hot_potato(['Bill', 'David', 'Susan', 'Sky', 'Brad', 'July'], 10)) for i in range(10): simulation(3600, 5)
利用Queue队列实现FIFO的算法
最新推荐文章于 2024-04-25 14:39:10 发布