Question: python Add preconditions to the methods __getitem__ and __setitem__ of the Array class. The precondition of each method is 0 Be sure to raise an

python Add preconditions to the methods __getitem__ and __setitem__ of the Arraypython

Add preconditions to the methods __getitem__ and __setitem__ of the Array class.

The precondition of each method is 0

Be sure to raise an IndexError exception if the precondition is not satisfied.

A main() has been provided for you.\

""" File: arrays.py

An Array is a restricted list whose clients can use only [], len, iter, and str.

To instantiate, use

= array(, )

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 placed at each position.""" self.items = list() for count in range(capacity): self.items.append(fillValue)

def __len__(self): """-> The capacity of the array.""" return len(self.items)

def __str__(self): """-> The string representation of the array.""" return str(self.items)

def __iter__(self): """Supports iteration over a view of an array.""" return iter(self.items)

def __getitem__(self, index): """Subscript operator for access at index.""" return self.items[index]

def __setitem__(self, index, newItem): """Subscript operator for replacement at index.""" self.items[index] = newItem

def main(): """Test code for modified Array class.""" a = Array(10) print("Physical size:", len(a)) print("Logical size:", a.size()) print("Items:", a) print(a[0])

if __name__ == "__main__": main()

Instructions Add preconditions to the methods _getitem__ and _setitem of the Array class. The precondition of each method is 0

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!