Question: Give the code for the following functions where the class below is given: def rectangle_eq(self, other), def rectangle_contains(self, p) and def rectangle_and(self, other). It must

Give the code for the following functions where the class below is given: def rectangle_eq(self, other), def rectangle_contains(self, p) and def rectangle_and(self, other). It must follow the exact parameters and pass the tests assigned to each function below. Insert code where it says, "YOUR CODE HERE."

import string

class Rectangle(object):

def __init__(self, *intervals, name=None):

"""A rectangle is initialized with a list, whose elements are

either Interval, or a pair of numbers.

It would be perhaps cleaner to accept only list of intervals,

but specifying rectangles via a list of pairs, with each pair

defining an interval, makes for a concise shorthand that will be

useful in tests.

Every rectangle has a name, used to depict it.

If no name is provided, we invent a random one."""

self.intervals = []

for i in intervals:

self.intervals.append(i if type(i) == Interval else Interval(*i))

# I want each rectangle to have a name.

if name is None:

self.name = ''.join(

random.choices(string.ascii_letters + string.digits, k=8))

else:

self.name = name

def __repr__(self):

"""Function used to print a rectangle."""

s = "Rectangle " + self.name + ": "

s += repr([(i.x0, i.x1) for i in self.intervals])

return s

def clone(self, name=None):

"""Returns a clone of itself, with a given name."""

name = name or self.name + "'"

return Rectangle(*self.intervals, name=name)

def __len__(self):

"""Returns the number of dimensions of the rectangle

(not the length of the edges). This is used with

__getitem__ below, to get the interval along a dimension."""

return len(self.intervals)

def __getitem__(self, n):

"""Returns the interval along the n-th dimension"""

return self.intervals[n]

def __setitem__(self, n, i):

"""Sets the interval along the n-th dimension to be i"""

self.intervals[n] = i

@property

def ndims(self):

"""Returns the number of dimensions of the interval."""

return len(self.intervals)

@property

def volume(self):

return np.prod([i.length for i in self.intervals])

def rectangle_eq(self, other):

### YOUR CODE HERE

Rectangle.__eq__ = rectangle_eq

Test

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 rectangle_and(self, other):

if self.ndims != other.ndims:

raise TypeError("The rectangles have different dimensions: {} and {}".format(

self.ndims, other.ndims

))

# Challenge: can you write this as a one-liner shorter than this comment is?

# There are no bonus points, note. Just for the fun.

### YOUR CODE HERE

Rectangle.__and__ = rectangle_and

Test

r1 = Rectangle((2, 3), (0, 4))

r2 = Rectangle((0, 4), (1, 3))

assert r1 & r2 == Rectangle((2, 3), (1, 3))

r1 = Rectangle((2, 3), (0, 4))

r2 = Rectangle((0, 4), (1, 5))

assert r1 & r2 == Rectangle((2, 3), (1, 4))

r1 = Rectangle((-1, 5), (0, 6))

r2 = Rectangle((0, 4), (-1, 3))

assert r1 & r2 == Rectangle((0, 4), (0, 3))

r1 = Rectangle((2, 6), (0, 4))

r2 = Rectangle((0, 6), (0, 3))

assert r1 & r2 == Rectangle((2, 6), (0, 3))

def rectangle_contains(self, p):

# The point is a tuple with one element per dimension of the rectangle.

if len(p) != self.ndims:

raise TypeError()

### YOUR CODE HERE

Rectangle.__contains__ = rectangle_contains

Test

assert (2, 3) in Rectangle((0, 4), (1, 5))

assert (0, 4) in Rectangle((0, 4), (4, 5))

assert (4, 5) in Rectangle((0, 4), (4, 5))

assert (0, 0, 0) not in Rectangle((3, 4), (0, 3), (0, 8))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!