amanpy >. Tarminal 38 ***Supports traversal with a for loop, return ster(self. stons) sendbas t oythond y. FIle "arrays.py", ine e for count in rare(len. (selt.itens), lentsels items) + 2): 41 defetiten.(elf. inden): 42 **Subseript operator for secess at index. 43 Precandition: e e index size() Syntatrror: invalid syntex rite your code hare sandbax ) 45 46 def setitem.(self. inder, etten) 47 **Subseript operator fer replacerent at index Precondition: e e index size() 40 Rrite your code hare 51 def stze(self): The nunbar of stans in the array. zaturn self.logicalsize 54 def grow(self): temparray l1at() far i in range(len(self.itoma)): tenearray. eppene(aelf.itena() self.itens temp.array far count in range(len. (self itens), lan(self.Stans) 2): self.itans.append(self.fillvalua) def sheink (self: 1f(self.capacity (len(self.itera)/2)): tenparray list() for i in zange(len(self.items)/r2): tenp.erray.append(self.ites(]) self.Itens tenp.array
Getting the attached error when inputting the below syntax...help
ORIGINAL QUESTION:
In the Array class of the arrays.py file complete the following:
- Add preconditions to the methods __getitem__ and __setitem__.
- The precondition of each method is 0 <= index < size().
- Be sure to raise an IndexError exception if the precondition is not satisfied.
To test your
Your program's output should look like the following:
Physical size: 10 Logical size: 0 Items: [None, None, None, None, None, None, None, None, None, None] Traceback (most recent call last): File ".solution/arrays.py", line 69, in <module> main() File ".solution/arrays.py", line 66, in main print(a[0]) File ".solution/arrays.py", line 45, in __getitem__ raise IndexError("Array index out of bounds") IndexError: Array index out of bounds
Adds a precondition on __getitem__ and __setitem__
and raises an exception if it is not satisfied.
An Array is a restricted list whose clients 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 placed at each position."""
self.items = list()
self.logicalSize = 0
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 traversal with a for loop."""
return iter(self.items)
def __getitem__(self, index):
"""Subscript operator for access at index.
Precondition: 0 <= index < size()"""
# Write your code here
def __setitem__(self, index, newItem):
"""Subscript operator for replacement at index.
Precondition: 0 <= index < size()"""
# Write your code here
def size(self):
"""-> The number of items in the array."""
return self.logicalSize
def grow(self):
temp_array = list()
for i in range(len(self.items)):
temp_array.append(self.items[i])
self.items = temp_array
for count in range(len.(self.items), len(self.items) * 2):
self.items.append(self.fillValue)
def shrink(self):
if(self.capacity <= (len(self.items)/2)):
temp_array = list()
for i in range(len(self.items)//2):
temp_array.append(self.items[i])
self.items = temp_array
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()
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images