Develop and test a Buffer ADT with the following description: class Buffer: def
ID: 3729499 • Letter: D
Question
Develop and test a Buffer ADT with the following description:
class Buffer:
def insert(self, c) # insert c at the cursor
def get(self) # return the character at the cursor
delete(self) # delete and return the character at the cursor
left (self, k) # move the cursor left k positions
right (self, k) # move the cursor right k positions
size(self) # return the number of characters in the buffer
Hint: use two stacks
USE PYTHON- USE STACKS!
Explanation / Answer
class Buffer: def __init__(self): self.left = [] self.right = [] def insert(self, c): # insert c at the cursor self.left.append(c) def get(self): # return the character at the cursor return self.left[-1] def delete(self): # delete and return the character at the cursor del self.left[-1] def left(self, k): # move the cursor left k positions for i in range(k): self.right.append(self.get()) self.delete() def right(self, k): # move the cursor right k positions for i in range(k): self.insert(self.right[-1]) del self.right[-1] def size(self): # return the number of characters in the buffer return len(self.left) + len(self.right)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.