Question: Need some help with python problem Test: Problem 2: Implementation of equality for SparseArrayDict As we saw in lecture, if we have two SparseArrayDict objects
Need some help with python problem

Test:

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 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. [ ] def sparse_array_dict_eq(self, other): Definition of equality for SparseArrayDict. It can be done in 9 lines of code." # YOUR CODE HERE raise Not ImplementedError() SparseArrayDict._eq__ = sparse_array_dict_eq [ ] ### Here, you are encouraged to write your own tests, to help you debug. ### You will not be graded on what you write here. # YOUR CODE HERE raise Not ImplementedError() [ ] a = SparseArrayDict(3, 4, 5) b = SparseArrayDict(3, 4, 5) C = SparseArrayDict(3, 5, 5) d = SparseArrayDict(3, 4, 5, 6) assert_equal(a, b) assert_not_equal(a, c) assert_not_equal(a, d) [] a = SparseArrayDict(3, 4, 5) b = SparseArrayDict(3, 4, 5, default=1) assert_not_equal(a, b) [] a = SparseArrayDict(3, 4, 5, size=10, default=1) b = SparseArrayDict(3, 4, 5, size=10, default=1) b[7] = 1 assert_equal(a, b) [ ] a = SparseArrayDict(2, 3, default=1) b = SparseArrayDict(2, 3, default=6) a[2] = 6 a[3] = 6 b[3] = 6 # This a and b should be considered not equal, # even though they happen to have the same elements now, # because they still have different default values # and therefore, could behave differently when extended. assert_not_equal(a, b)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
