Question: python coding.... Need help getting the task 3 to work... Here is what I have done so far: from scipy import * from pylab import
python coding....
Need help getting the task 3 to work... Here is what I have done so far:
from scipy import *
from pylab import *
from numpy import *
#TASK 1
class Interval:
def __init__(self, a, b=None):
if b==None:
b=left
self.a=a
self.b=b
#TASK 2
def __add__(self,other):
a,b = self.a, self.b
if isinstance(other, int) or isinstance(other, float):
c,d= other,other
return Interval(a+c, b+d)
if isinstance(other, Interval):
c,d = other.a,other.b
return Interval(a+c, b+d)
def __radd__(self, other):
return self + other
def __sub__(self, other):
a,b=self.a, self.b
if isinstance(other, int) or isinstance(other, float):
c,d = other,other
return Interval(a-d, b-c)
elif isinstance(other,Interval):
c, d = other.a, other.b
return Interval(a-d, b-c)
return Interval(a-d, b-c)
def __rsub__(self,other):
if isinstance(other, int) or isinstance(other,float):
return (Interval(other)-self)
def __mul__(self,other):
a,b=self.a,self.b
if isinstance(other, int) or isinstance(other,float):
c,d=other,other
return (Interval(min(a*c,a*d,b*c,b*d),max(a*c,a*d,b*c,b*d))
elif isinstance(other, Interval):
c,d=other.a, other.b
return (Interval(min(a*c,a*d,b*c,b*d),max(a*c,a*d,b*c,b*d))
def __rmul__(self,other):
if isinstance(other,int) or isinstance(other,float):
return (self*other)
def __truediv__(self,other):
a,b=self.a,self.b
if isinstance(other, int) or isinstance(other,float):
c,d=other,other
if c*d==0:
raise ValueError('0 in denominator' % other)
else:
return (Interval(min(a/c,a/d,b/c,b/d),max(a/c,a/d,b/c,b/d))
elif isinstance(other,Interval):
c,d=other.a, other.b
if c*d==0:
raise ValueError('0 in denominator' % other)
else:
return (Interval(min(a/c,a/d,b/c,b/d),max(a/c,a/d,b/c,b/d))
def __repr__(self):
return "[{},{}]".format(self.a,self.b)




Task 1 Construct a class Interval which is initialized with two real numbers representing the left and right endpoints respectively. Task 2 Provide methods for the four basic arithmetic operations Task 3 Provide a print method so that the code Interval (1,2) prints 1, 2]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
