In: Computer Science
python 3
We would like to add cursor-based APIs to the array-backed list API. To this end, we are including a cursor attribute, and two related methods. cursor_set will set the cursor to its argument index, and cursor_insert will insert its argument value into the list at the current cursor position and advance the cursor by 1.
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_insert should behave like append.
Programming rules:
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
Program:
Output:
Summary:
Method cursor_insert(self,val) accepts one paramater val . The val should be inserted into the current position of the curser_set.
If the cursor is the length of the Arraylist i.e, at the last element then:
Function acts as append
Else using insert(index,element) method it inserts the val into the cursor point.