Question: import string class Rectangle ( object ) : def _ _ init _ _ ( self , * intervals , name = None ) :

import string
class Rectangle(object):
def __init__(self,*intervals, name=None): [29] # Whooho!! Let's try with another example. Now T is inside R.
.
"""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
def __eq__(self, other):
if not isinstance(other, Rectangle):
return False
# We rely on interval equality here.
return self.intervals == other.intervals
@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])
import string class Rectangle ( object ) : def _

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 Programming Questions!