Question

In: Computer Science

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

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:

  • You should only add code to the cursor_insert method; the rest of 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-in Python list as you did in the ArrayList lab.
  • Assume that cursor is set to a value between 0 and the length of the list (inclusive) when cursor_insert is called.
  • You may not use any other data structures (this includes the built-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

Solutions

Expert Solution

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.


Related Solutions

Add Instance to the Array List Instances in an Array Search for an Instance in the...
Add Instance to the Array List Instances in an Array Search for an Instance in the Array Delete an Instance from the Array Update an Instance in the Array Save Array Elements to CSV File Exit Program The requirement of this assignment is to design and implement a system in Java that will manage instantiated instances of the Objects created in the User Designed Object Assignment. This is the Young Adult Object. The system should be able to add an...
E.g., given an array-backed list lst containing the elements [0, 1, 2, 3, 4, ..., 98,...
E.g., given an array-backed list lst containing the elements [0, 1, 2, 3, 4, ..., 98, 99], the following code: for x in lst.poly_iter(2, 3, 4): print(x) will produce the output: 4 9 18 31 48 69 94 Programming rules: You must adhere to the same rules when using the built-in Python list as you did in the ArrayList lab. You may not use any other data structures (this includes the built-in Python list). class ArrayList: def __init__(self): self.data =...
Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove,...
C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove, and list. This assignment is to extend the functionalities of the bag with other operations average, min, and max, You need to extend the Bag class (under Wk 2, BagLinked_List.cpp) with the following methods: -int Bag::min( ), is to find minimum of items of the bag. -int Bag::max( ), is to find maximum of items of the bag -float Bag::ave( ), is to find...
Please make an Array-based implementation of a Binary Tree in Python based on the given file...
Please make an Array-based implementation of a Binary Tree in Python based on the given file below. Make sure to keep the Abstract Data File of the starter code below when building the Array-based implementation. Python Starter Code Given Below: class ArrayBinaryTree(BinaryTree): """Linked representation of a binary tree structure.""" # -------------------------- nested _Node class -------------------------- class _Node: def __init__(self, element, parent=None, left=None, right=None): # -------------------------- nested Position class -------------------------- class Position(BinaryTree.Position): """An abstraction representing the location of a single element."""...
What is an array-based list? What is a resizable list? What is the difference between a...
What is an array-based list? What is a resizable list? What is the difference between a list’s capacity and its size? When a list is expanded, is the size changed or is its capacity changed?
Using Python:     "We have a list of strings: \n",     "```python\n",     "CharList = ['a',...
Using Python:     "We have a list of strings: \n",     "```python\n",     "CharList = ['a', 'b', 'c', 'd']\n",     "```\n",     "Now, we write a program to delete some elements from the list. <br> \n",     "The indexes of the elements to be deleted are stored in IndexList <br>\n",     "\n",     "```Scenario-1```: IndexList = [3, 0]\n",     "```python\n",     "CharList = ['a', 'b', 'c', 'd']\n",     "IndexList = [3, 0]\n",     "for n in range(0, len(IndexList)):    \n",    ...
python coding Suppose a list of positive numbers is given like the following list (remember this...
python coding Suppose a list of positive numbers is given like the following list (remember this is only an example and the list could be any list of positive numbers) exampleList: 15 19 10 11 8 7 3 3 1 We would like to know the “prime visibility” of each index of the list. The “prime visibility” of a given index shows how many numbers in the list with indexes lower than the given index are prime. For instance, in...
Convert arrays.py to Java. ‘’’’’’ File: arrays.py An Array is like a list, but the client...
Convert arrays.py to Java. ‘’’’’’ File: arrays.py An Array is like a list, but the client can use Only [], len, iter, and str. To instantiate, use <variable> = Array(<capacity>, <optional fill value>) The fill value is None by default. ‘’’’’’ class Array(object): ‘’’’’’Represents an array.’’’’’’ def__init__(self, capacity, fillValue = None):                ‘’’’’’Capacity is the static size of the array.                fillValue is place at each position.’’’’’’                self.items = list()                for count in range(capacity):                self.items.append(fillValue) def__len__(self):               ...
for Python 3 Write a python program that Creates a list that has these values in...
for Python 3 Write a python program that Creates a list that has these values in this order, 'Python', 'JavaScript', and 'PHP' Define a displayMyClasses function that sorts the list and then displays each item on the list, one per line, in sorted order. Also, number the displayed list as shown in the "Running a Sample Program" shown below. Define a guessNext function that selects a random element from the list and returns it to the call of the function....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT