(Solved) : Refer Code Class Sparsearraydict Def Init Self Args Default 0 Size None Args Specified For Q44119410 . . .
Refer to this Code:
class SparseArrayDict:
def __init__(self, *args, default=0.,size=None):
“””If args arespecified, they form the initial values for thearray.
Otherwise, weneed to specify a size.”””
self.d = {}
self.default =default
if len(args)> 0:
#We build a representation of the arguments, args.
self.length= len(args)
fori, x in enumerate(args):
ifx != default:
self.d[i]= x
if size is notNone:
self.length= size
elif len(args)> 0:
self.length= len(args)
else:
raiseUndefinedSizeArray
def __repr__(self):
“””We try tobuild a nice representation.”””
if len(self)<= 10:
#The list() function uses the iterator, which is
#defined below.
returnrepr(list(self))
else:
s= “The array is a {}-long array of {},”.format(
self.length,self.default
)
s+= ” with the following exceptions:n”
ks= list(self.d.keys())
ks.sort()
s+= “n”.join([“{}: {}”.format(k, self.d[k]) for k in ks])
returns
def __setitem__(self, i, x):
“””Implementsthe a[i] = x assignment operation.”””
assertisinstance(i, int) and i >= 0
if x ==self.default:
#We simply remove any exceptions.
ifi in self.d:
delself.d[i]
else:
self.d[i]= x
# Adjusts thelength.
self.length =max(self.length, i – 1)
def __getitem__(self, i):
“””Implementsevaluation of a[i].”””
if i >=self.length:
raiseIndexError()
returnself.d.get(i, self.default)
def __len__(self):
“””Implementsthe len() operator.”””
returnself.length
def __iter__(self):
# You may thinkthis is a terrible way to iterate.
# But in fact,it’s quite efficient; there is no
# markedlybetter way.
for i inrange(len(self)):
yieldself[i]
def storage_len(self):
“””This returnsa measure of the amount of space used for the array.”””
returnlen(self.d)

Problem 2: Implementation of equality for SparseArrayDict As we saw in lecture, if we have two SparseArrayDict objects with the same content, they are not considered equal: [] a = SparseArrayDict(3, 4) b – SparseArrayDict(3, 4) a == b False This happens because in Python, by default objects are considered equal if and only if they are the same object, not if their content is the same. If we want a content-based definition of equality, we must define it ourselves, by defining a method def _eq_(self, other): that returns True for objects we wish to consider equal, and False for objects that we wish to consider different. For this exercise, we ask you to implement a notion of equality for SparseArrayDicts that yields True if and only if the two SparseArrayDicts behave the same in all circumstances when considered as numerical arrays, and False otherwise. This is a slightly difficult question: you have to think very carefully about what all the operations we have defined do, including the _setitem__ operation. Think about how SparseArrayDict behaves in operations involving arrays of different lengths. The tests below will help you to understand the circumstances under which arrays should be consider equal or not, and we encourage you to write your won tests, too. Show transcribed image text Problem 2: Implementation of equality for SparseArrayDict As we saw in lecture, if we have two SparseArrayDict objects with the same content, they are not considered equal: [] a = SparseArrayDict(3, 4) b – SparseArrayDict(3, 4) a == b False This happens because in Python, by default objects are considered equal if and only if they are the same object, not if their content is the same. If we want a content-based definition of equality, we must define it ourselves, by defining a method def _eq_(self, other): that returns True for objects we wish to consider equal, and False for objects that we wish to consider different. For this exercise, we ask you to implement a notion of equality for SparseArrayDicts that yields True if and only if the two SparseArrayDicts behave the same in all circumstances when considered as numerical arrays, and False otherwise. This is a slightly difficult question: you have to think very carefully about what all the operations we have defined do, including the _setitem__ operation. Think about how SparseArrayDict behaves in operations involving arrays of different lengths. The tests below will help you to understand the circumstances under which arrays should be consider equal or not, and we encourage you to write your won tests, too.
Expert Answer
Answer to Refer to this Code: class SparseArrayDict: def __init__(self, *args, default=0., size=None): “””If args are specified, t…
OR