Question: Task 1 [ 9 marks] Code in PYTHON Implement a complete version of an Array-Based List. Use 50 as the maximum number of elements. To

Task 1 [9 marks] Code in PYTHON

Implement a complete version of an Array-Based List. Use 50 as the maximum number of elements. To create arrays use the method build_array from referential_array.py as discussed in the Lectures. Your List should include implementations for the following 10 functions:

__str__(self): Returns a string representation of the list. Structure the string so that there is one item per line. Called by str(self)

__len__(self): Returns the length of the list. Called by len(self)

__contains__(self, item): Returns True if item is in the list, False otherwise. Called by item in self

__getitem__(self, index): Returns the item at index in the list, if index is non-negative. If it is negative, it will return the last item if index is 1, the second-to last if index is 2, and so on up to minus the length of the list, which returns the first item. The function raises an IndexError if index is out of the range from -len(self) to len(self). Called by self[index]

__setitem__(self, index, item): Sets the value at index in the list to be item. The index can be negative, behaving as described above. Raises an IndexError if index is out of the range from-len(self) to len(self). Called by self[index] = item

__eq__(self, other): Returns True if this list is equivalent to other. Called by self == other

append(self, item): Adds item to the end of the list. Remember the underlying array is and should

remain of fixed size. The operation should raise an Exception if the list is full.

insert(self, index, item): Inserts item into self before position index. The index can be negative, behaving as described above. Raises an IndexError if index is out of the range from -len(self) tolen(self).

remove(self, item): Deletes the first instance of item from the list. Raises a ValueError if item does not exist in self

delete(self, index): Deletes the item at index from the list, moving all items after it towards the start of the list. The index can be negative, behaving as described above. Raises an IndexError if index is out of the range from -len(self) to len(self).

sort(self, reverse): Sorts the items in the list in ascending order if reverse is False or descending order if is reverse is True. Pick your favourite sorting algorithm from those covered in the Lectures.

Make your class iterable.

Code for referential_array.py (https://repl.it/@MuhammadFermi/week6-2)

import ctypes

def build_array(size):

"""

This function creates an array of references to Python Objects.

Args:

size (int): A positive integer, the size of the array.

Returns:

An array of python references with the given size.

"""

if size <= 0:

raise ValueError("Array size should be larger than 0.")

if not isinstance(size, int):

raise ValueError("Array size should be an integer.")

array = (size * ctypes.py_object)()

array[:] = size * [None]

return array

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!