Question: Your ArrayList lab implementation failed to account for list slice arguments- e.g., lst [1:5]. Working with slices is easy, as Python automatically creates slice objects

Your ArrayList lab implementation failed to account for list slice arguments- e.g., "lst [1:5]". Working with slices is easy, as Python automatically creates slice objects and passes them to the__getitem__, __setitem_-, and __delitem__ methods. slice objects have start and stop attributes that give us the values specified in the array bracket notation. Below is an implementation of__getitem_ that accepts both regular integer indexes and slices class ArrayList: def _init__(self): self, data = [] def __getitem_.(self, idx): if not isinstance(idx, slice): return self, data[idx] # deal with integer indices else: return [self [i] for i in range(idx.start, idx.stop)] Now, with an ArrayList named 1 whose data attribute refers to the list [1, 2, 3, 4, 5], the expression 1[1:4] evaluates to [2, 3, 4] WP3 Complete the implementations of__setitem__and__delitem_so as to accept slices. You may assume all slices contain only valid, positive index values, and do not include a step (i.e., you need not support slices of the form 1[start:stop:stepl) As in the lab, you should treat the underlying Python list (in data) as an array. You may assume that the ArrayList class has working append, insert, and __getitem__ methods (not shown) The following code and accompanying output demonstrates how slices may be used with setitem__ and __delitem__: 1-ArrayList) for x in range(10): print ('1:', 1) print ('2:', 1) 1.append (x) del 1[2:4] print ('3:', 1) # Output #1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # 3: [0, ,a, , 9]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
