{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", "Queue() creates a new queue that is empty. It needs no parameters and returns an empty queue.\n", "enqueue(item) adds a new item to the rear of the queue. It needs the item and returns nothing.\n", "dequeue() removes the front item from the queue. It needs no parameters and returns the item. The queue is modified.\n", "isEmpty() tests to see whether the queue is empty. It needs no parameters and returns a boolean value.\n", "size() returns the number of items in the queue. It needs no parameters and returns an integer.\n", "\"\"\"\n", "class Queue:\n", " def __init__(self):\n", " self.items = []\n", "\n", " def isEmpty(self):\n", " return self.items == []\n", "\n", " def enqueue(self, item):\n", " self.items.insert(0,item)\n", "\n", " def dequeue(self):\n", " return self.items.pop()\n", "\n", " def size(self):\n", " return len(self.items)\n", " \n", "\"\"\"\n", "Simulation: Hot Potato\n", "\n", "In this game children line up in a circle and pass an item from neighbor to neighbor as fast as they can. \n", "At a certain point in the game, the action is stopped and the child who has the item (the potato) is removed from the circle. \n", "Play continues until only one child is left.\n", "\n", "\"\"\"\n", "def hotPotato(namelist, num):\n", " simqueue = Queue()\n", " for name in namelist:\n", " simqueue.enqueue(name)\n", "\n", " while simqueue.size() > 1:\n", " for i in range(num):\n", " simqueue.enqueue(simqueue.dequeue()) # take away the first one, add it to the end, then a loop forms\n", "\n", " simqueue.dequeue()\n", "\n", " return simqueue.dequeue()\n", "\n", "print(hotPotato([\"Bill\",\"David\",\"Susan\",\"Jane\",\"Kent\",\"Brad\"],7))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Queue:\n", " def __init__(self):\n", " self.items = []\n", "\n", " def isEmpty(self):\n", " return self.items == []\n", "\n", " def enqueue(self, item):\n", " self.items.insert(0,item)\n", "\n", " def dequeue(self):\n", " return self.items.pop()\n", "\n", " def size(self):\n", " return len(self.items)\n", "\n", "\"\"\"\n", "Simulation: Printing Tasks\n", "As students send printing tasks to the shared printer, the tasks are placed in a queue to be processed in a first-come first-served manner.\n", "The most important of these might be whether the printer is capable of handling a certain amount of work. \n", "If it cannot, students will be waiting too long for printing and may miss their next class.\n", "\"\"\"\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3.14" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }