Question
PYTHON URGENT help PLZ My assignment is due very soon and no one has answered my previous question. I have to create a method for
PYTHON URGENT help PLZ
My assignment is due very soon and no one has answered my previous question. I have to create a method for python that compares the dimensions of a rectangle to another.
Two rectangles and are equal if they have the same number of dimensions, and if for every dimension , the interval of along is equal to the interval of along . It has to pass given tests.
def rectangle_eq(self, other):
### YOUR CODE HERE
Rectangle.__eq__ = rectangle_eq
# 5 points: tests for rectangle equality.
assert Rectangle((2, 3), (4, 5)) == Rectangle((2, 3), (4, 5))
assert Rectangle((2, 3), (4, 5)) != Rectangle((4, 5), (2, 3))
assert Rectangle((2, 3), (4, 5)) != Rectangle((2, 3), (4, 5), (6, 7))
def interval_or (self, other): "Union of self and other. Returns a list of 1 or 2 non-overlapping intervals.""" if self.xl = self.x0 and x self.xl: return [Interval(self.xo, self.xl)] other_copy = copy.deepcopy (other) other_copy.x0 = max(other_copy.xo, self.x0) other_copy.x1 = min (other_copy.xl, self.xl) answer = [] if other_copy.x0 > self.x0 : answer.append( Interval(self.xo, other_copy.x0)) if other_copy.xl 0 else (0., 1.) loy, hiy - (min(lo_y), max(hi y)) if len(loy) > 0 else (0., 1.) sx, sy = hix - lox, hiy - loy lox -- 0.2 * SX hix += 0,2 * * loy -= 0.2 sy hiy +- 0.2 sy ax.set_xlim(lox, hix) ax.set_ylim(loy, hiy) plt.gca().set_aspect('equal', adjustable='box') plt.grid) plt.show() Url = Rectangle(13., 5.), (1., 4.), name="A") r2 - Rectangle((1., 4.), (2., 6.), name='2") r3 = Rectangle(2., 3.5), (1.5, 5.), name="C") draw_rectangles (r1, r2, 13) We let you implement rectangle equality, intersection, and membership of a point in a rectangle. Equality: Two rectangles R and T are equal if they have the same number of dimensions, and if for every dimension k, the interval of R along k is equal to the interval of T along k. For example, Rectangle( (2, 3), (4, 5)) == Rectangle( (2, 3), (4, 5)) Rectangle((2, 3), (4, 5)) != Rectangle((4, 5), (2, 3)) Rectangle((2, 3), (4, 5)) != Rectangle((2, 3), (4, 5), (6, 7)) Intersection: The intersection is defined only if the rectangles have the same number of dimensions. The intersection is computed by taking the intersection of the intervals of the two rectangles for corresponding dimensions. Membership: For an n-dimensional point (x0, X1, ... ,xn) and an n-dimensional rectangle R, we have (x0, X1, ... , Xn) E R if the point is in the region R. For instance: (2.5, 4.5) in Rectangle( (2, 3), (4, 5)) (2, 3) in Rectangle((2, 3), (4, 5)) (1, 3) not in Rectangle((2, 3), (4, 5)) If the point and the rectangle have different dimensions, you can raise a TypeError. TT B I ## Question 5: Rectangle Equality U def rectangle_eg(self, other): ### YOUR CODE HERE Rectangle. eg = rectangle_eq [ 5 points: tests for rectangle equality. assert Rectangle( (2, 3), (4, 5)) == Rectangle((2, 3), (4, 5)) assert Rectangle((2, 3), (4, 5)) != Rectangle((4, 5), (2, 3)) assert Rectangle((2, 3), (4, 5)) != Rectangle((2, 3), (4, 5), (6, 7)) def interval_or (self, other): "Union of self and other. Returns a list of 1 or 2 non-overlapping intervals.""" if self.xl = self.x0 and x self.xl: return [Interval(self.xo, self.xl)] other_copy = copy.deepcopy (other) other_copy.x0 = max(other_copy.xo, self.x0) other_copy.x1 = min (other_copy.xl, self.xl) answer = [] if other_copy.x0 > self.x0 : answer.append( Interval(self.xo, other_copy.x0)) if other_copy.xl 0 else (0., 1.) loy, hiy - (min(lo_y), max(hi y)) if len(loy) > 0 else (0., 1.) sx, sy = hix - lox, hiy - loy lox -- 0.2 * SX hix += 0,2 * * loy -= 0.2 sy hiy +- 0.2 sy ax.set_xlim(lox, hix) ax.set_ylim(loy, hiy) plt.gca().set_aspect('equal', adjustable='box') plt.grid) plt.show() Url = Rectangle(13., 5.), (1., 4.), name="A") r2 - Rectangle((1., 4.), (2., 6.), name='2") r3 = Rectangle(2., 3.5), (1.5, 5.), name="C") draw_rectangles (r1, r2, 13) We let you implement rectangle equality, intersection, and membership of a point in a rectangle. Equality: Two rectangles R and T are equal if they have the same number of dimensions, and if for every dimension k, the interval of R along k is equal to the interval of T along k. For example, Rectangle( (2, 3), (4, 5)) == Rectangle( (2, 3), (4, 5)) Rectangle((2, 3), (4, 5)) != Rectangle((4, 5), (2, 3)) Rectangle((2, 3), (4, 5)) != Rectangle((2, 3), (4, 5), (6, 7)) Intersection: The intersection is defined only if the rectangles have the same number of dimensions. The intersection is computed by taking the intersection of the intervals of the two rectangles for corresponding dimensions. Membership: For an n-dimensional point (x0, X1, ... ,xn) and an n-dimensional rectangle R, we have (x0, X1, ... , Xn) E R if the point is in the region R. For instance: (2.5, 4.5) in Rectangle( (2, 3), (4, 5)) (2, 3) in Rectangle((2, 3), (4, 5)) (1, 3) not in Rectangle((2, 3), (4, 5)) If the point and the rectangle have different dimensions, you can raise a TypeError. TT B I ## Question 5: Rectangle Equality U def rectangle_eg(self, other): ### YOUR CODE HERE Rectangle. eg = rectangle_eq [ 5 points: tests for rectangle equality. assert Rectangle( (2, 3), (4, 5)) == Rectangle((2, 3), (4, 5)) assert Rectangle((2, 3), (4, 5)) != Rectangle((4, 5), (2, 3)) assert Rectangle((2, 3), (4, 5)) != Rectangle((2, 3), (4, 5), (6, 7))
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