Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USE PYTHON3; DO NOT IMPORT ANY PACKAGES Here's the implemented Queue the problem is talking about: class Collection: A class to abstract the common

USE PYTHON3; DO NOT IMPORT ANY PACKAGES

image text in transcribed

Here's the implemented Queue the problem is talking about:

class Collection: """ A class to abstract the common functionalities of Stack and Queue. This class should not be initialized directly. """

def __init__(self): """ Constructor. """ self.items = [] self.num_items = 0

def size(self): """ Get the number of items stored. """ return self.num_items

def is_empty(self): """ Check whether the collection is empty. """ return self.num_items == 0

def clear(self): """ Remove all items in the collection. """ self.items = [] self.num_items = 0

class Queue(Collection):

def enqueue(self, item): """ Enqueue `item` to the queue. """ if item == None: raise ValueError("item cannot be None") self.num_items +=1 self.items.append(item)

def dequeue(self): """ Dequeue the front item from the queue. """ if self.num_items == 0: return None self.num_items -= 1 return self.items.pop(0)

def peek(self): """ Peek the front item. """ if self.num_items == 0: return None return self.items[0]

def __str__(self): """ Return the string representation of the queue. """ strings = map(str, self.items) if self.num_items == 0: return ("(front) " + "(rear)") return ("(front) " + " -- ".join(strings) + " (rear)")

Write a generator that takes in an iterable object and infinitely yields its items. This generator will start by yielding the first item, then it will repeat the following procedures: (1) Skip items following the yielded item by an increasing amount (starts from O, increase by 1 once yielded an item). If this generator reaches the end of the iterable when skipping, it will proceed from the front of the iterable and keep skipping. (2) Yield the item. You must use the Queue you just implemented to solve this problem. Think about how you can utilize the Queue operations in order to infinitely iterate through the object, yielding items whenever necessary. Example: iterable = range(10) # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 0123456789012345678901234567890123456789012345678901234567890123456789 A AA yield o, skip nothing, yield 1, skip (2), yield 3, skip (4, 5), yield 6, skip (7, 8, 9), yield o, skip (1, 2, 3, 4), yield 5, skip (6, 7, 8, 9, 0), yield 1, skip (2, 3, 4, 5, 6, 7), yield 8, skip (9, 0, 1, 2, 3, 4, 5), yield 6, skip (7, 8, 9, 0, 1, 2, 3, 4), yield 5, ... def inf_skip_increasing(iterable): >>> gen = inf_skip_increasing (range (10) >>> next(gen) 0 >>> next(gen) 1 >>> [next(gen) for - in range (10)] [3, 6, 0, 5, 1, 8, 6, 5, 5, 6] # YOUR CODE GOES HERE #

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions

Question

1 The purposes, tools, and limitations of fiscal policy.

Answered: 1 week ago