Question: PYTHON ONLY PLEASE FOLLOW DIRECTIONS! I REALLY WANT TO UNDERSTAND HOW THIS WORKS BY RUNNING IT MYSELF ring_buffer.py Models a ring buffer. import
PYTHON ONLY PLEASE FOLLOW DIRECTIONS! I REALLY WANT TO UNDERSTAND HOW THIS WORKS BY RUNNING IT MYSELF
""" ring_buffer.py
Models a ring buffer. """
import stdarray import stdio import sys
def create(capacity): """ Create and return a ring buffer, with the given maximum capacity and with all elements initialized to None. A ring buffer is represented as a list of four elements: the buffer (buff) itself as a list; number of elements (size) currently in buff; the index (first) of the least recently inserted item; and the index (last) one beyond the most recently inserted item. """
...
def capacity(rb): """ Return the capacity of the ring buffer. """
...
def size(rb): """ Return the number of items currently in the buffer rb. """
...
def is_empty(rb): """ Return True if the buffer rb is empty and False otherwise. """
...
def is_full(rb): """ Return True if the buffer rb is full and False otherwise. """
...
def enqueue(rb, x): """ Add item x to the end of the buffer rb. """
...
def dequeue(rb): """ Delete and return item from the front of the buffer rb. """
...
def peek(rb): """ Return (but do not delete) item from the front of the buffer rb. """
...
def _main(): """ Test client [DO NOT EDIT]. """
N = int(sys.argv[1]) rb = create(N) for i in range(1, N + 1): enqueue(rb, i) t = dequeue(rb) enqueue(rb, t) stdio.writef('Size after wrap-around is %d ', size(rb)) while size(rb) >= 2: x = dequeue(rb) y = dequeue(rb) enqueue(rb, x + y) stdio.writeln(peek(rb))
if __name__ == '__main__': _main()
Your first task is to model the ring buffer. Write a module ring_buffer.py that implements the following API: Since the ring buffer has a known maximum capacity, we implement it as a list (buff) of floats of that length, with the number of elements in the buffer stored in size. For efficiency, we use cyclic wrap-around, which ensures that each operation can be done in a constant amount of time. We maintain an index first that stores the index of the least recently inserted item, and an index last that stores the index one beyond the most recently inserted item. To insert an item into the buffer, we put it at index last and increment last. To remove an item from the buffer, we take it from index first and increment first. When either index equals capacity, we make it wrap around by changing the index to 0. The ring buffer can thus be represented as a list of four elements: the buffer (buff) number of elements (size) currently in buff; the index (first) of the least recently inserted item; and the index (last) one beyond the most recently inserted item. For example, the ring buffer shown in the figure below can be represented as the list [[middot, middot, 0.5, 0.3, -0.2, 0.4, ]. Calling enqueue() on a full buffer should terminate the program with the message "Error: cannot enqueue a full buffer Calling peek() or de queue() on an empty buffer should terminate the program with the message "Error: cannot peek an empty buffer or "Error: cannot dequeue an empty buffer". Use sys.exit(msg) to terminate a program with the message msg. python. ring_buffer py 10 Size after wrap _around is 10 55 $ python. ring_buffer.py 100 Size after wrap-around is 100 5050 Your first task is to model the ring buffer. Write a module ring_buffer.py that implements the following API: Since the ring buffer has a known maximum capacity, we implement it as a list (buff) of floats of that length, with the number of elements in the buffer stored in size. For efficiency, we use cyclic wrap-around, which ensures that each operation can be done in a constant amount of time. We maintain an index first that stores the index of the least recently inserted item, and an index last that stores the index one beyond the most recently inserted item. To insert an item into the buffer, we put it at index last and increment last. To remove an item from the buffer, we take it from index first and increment first. When either index equals capacity, we make it wrap around by changing the index to 0. The ring buffer can thus be represented as a list of four elements: the buffer (buff) number of elements (size) currently in buff; the index (first) of the least recently inserted item; and the index (last) one beyond the most recently inserted item. For example, the ring buffer shown in the figure below can be represented as the list [[middot, middot, 0.5, 0.3, -0.2, 0.4, ]. Calling enqueue() on a full buffer should terminate the program with the message "Error: cannot enqueue a full buffer Calling peek() or de queue() on an empty buffer should terminate the program with the message "Error: cannot peek an empty buffer or "Error: cannot dequeue an empty buffer". Use sys.exit(msg) to terminate a program with the message msg. python. ring_buffer py 10 Size after wrap _around is 10 55 $ python. ring_buffer.py 100 Size after wrap-around is 100 5050
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
