印刷ジョブのキューを作成する
前のビデオでは、キューがプリンターの印刷ジョブ管理など、さまざまな場面で活用できることを学びました。
この演習では、プリンターのキューを簡略化したPrinterTasks()クラスを作成しましょう。以下のメソッドを持つQueue()クラスを使用します。
enqueue(data):キューに要素を追加するdequeue():キューから要素を取り出すhas_elements():キューに要素があるかどうかを確認する。
コードは以下のとおりです。
def has_elements(self):
return self.head != None
まず、add_document()メソッドとprint_documents()メソッドを実装したPrinterTasks()クラスのコーディングを始めましょう。その後、PrinterTasks()クラスを使ったプログラムを実行してシミュレーションします。
この演習はコースの一部です
Pythonで学ぶデータ構造とアルゴリズム
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
class PrinterTasks:
def __init__(self):
self.queue = Queue()
def add_document(self, document):
# Add the document to the queue
self.____.____(document)
def print_documents(self):
# Iterate over the queue while it has elements
while self.____.____():
# Remove the document from the queue
print("Printing", self.____.____())