Question: import string class Rectangle ( object ) : def _ _ init _ _ ( self , * intervals , name = None ) :
import string
class Rectangleobject:
def initselfintervals nameNone: # Whooho!! Let's try with another example. Now 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.appendi if typei Interval else Intervali
# I want each rectangle to have a name.
if name is None:
self.name join
random.choicesstringasciiletters string.digits, k
else:
self.name name
def reprself:
Function used to print a rectangle."""
s "Rectangle self.name :
s reprix ix for i in self.intervals
return s
def cloneself nameNone:
Returns a clone of itself, with a given name."""
name name or self.name
return Rectangleselfintervals, namename
def lenself:
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 lenselfintervals
def getitemself n:
Returns the interval along the nth dimension"""
return self.intervalsn
def setitemself n i:
Sets the interval along the nth dimension to be i
self.intervalsn i
def eqself other:
if not isinstanceother Rectangle:
return False
# We rely on interval equality here.
return self.intervals other.intervals
@property
def ndimsself:
Returns the number of dimensions of the interval."""
return lenselfintervals
@property
def volumeself:
return npprodilength for i in self.intervals
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
