Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python refer to this previous question for the code https://www.chegg.com/homework-help/questions-and-answers/python-problem-1-implementation-arithmetic-operations-class-undefinedsizearray-exception-p-q44162395?trackid=5phQCv34 Problem 2: Implementation of equality for SparseArrayDict As we saw in lecture, if we have
Python
refer to this previous question for the code
https://www.chegg.com/homework-help/questions-and-answers/python-problem-1-implementation-arithmetic-operations-class-undefinedsizearray-exception-p-q44162395?trackid=5phQCv34
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: [57] 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. [58] def sparse_array_dict_eg(self, other): "" "Definition of equality for SparseArrayDict. It can be done in 1-2 lines of code.""" return self.default == self.other # YOUR CODE HERE raise NotImplementedError() [] 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). 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: [57] 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. [58] def sparse_array_dict_eg(self, other): "" "Definition of equality for SparseArrayDict. It can be done in 1-2 lines of code.""" return self.default == self.other # YOUR CODE HERE raise NotImplementedError() [] 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started