Need help python " Write an Array Data Structure which has a constructor, "len", "str", "itr", "getitem" and "setitem" methods. Then create an object of this Array class with 6 elements in it. Fill the object of this class with numbers from 3 to 8. print the array and show the length of the array object." Here is the array code """ 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 traversal with a for loop.""" 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
Need help python
"
Write an Array Data Structure which has a constructor, "len", "str", "itr", "getitem" and "setitem" methods.
Then create an object of this Array class with 6 elements in it. Fill the object of this class with numbers from 3 to 8.
print the array and show the length of the array object."
Here is the array code
"""
File: arrays.py
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()
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."""
return self.items[index]
def __setitem__(self, index, newItem):
"""Subscript operator for replacement at index."""
self.items[index] = newItem
Step by step
Solved in 4 steps with 1 images