python 3 We would like to add cursor-based APIs to the array-backed list API. To this end,...

90.2K

Verified Solution

Question

Programming

python 3

We would like to add cursor-based APIs to the array-backed listAPI. To this end, we are including a cursor attribute, and tworelated methods. cursor_set will set the cursor to its argumentindex, and cursor_insert will insert its argument value into thelist at the current cursor position and advance the cursor by1.

E.g, given an array-backed list l that contains the values [0,1, 2, 3, 4, 5, 6, 7, 8, 9], running the following code

 l.cursor_set(5) l.cursor_insert('a') l.cursor_insert('b') l.cursor_insert('c')

will result in the updated list [0, 1, 2, 3, 4, 'a', 'b', 'c',5, 6, 7, 8, 9]

When the cursor is set to the length of the list, cursor_insertshould behave like append.

Programming rules:

  • You should only add code to the cursor_insert method; the restof ArrayList is provided for reference and testing purposes only.cursor_set is already provided.
  • You must adhere to the same rules when using the built-inPython list as you did in the ArrayList lab.
  • Assume that cursor is set to a value between 0 and the lengthof the list (inclusive) when cursor_insert is called.
  • You may not use any other data structures (this includes thebuilt-in Python list).

class ArrayList:
def __init__(self):
self.data = []
self.cursor = 0
  
def append(self, val):
self.data.append(None)
self.data[len(self.data)-1] = val
  
def cursor_set(self, idx):
self.cursor = idx
  
def cursor_insert(self, val):
# YOUR CODE HERE

Answer & Explanation Solved by verified expert
4.1 Ratings (748 Votes)
ProgramOutputSummaryMethod cursorinsertselfval accepts one    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students