Python class from goody import type_as_str from math import sqrt class Interval:
ID: 3604632 • Letter: P
Question
Python class
from goody import type_as_str
from math import sqrt
class Interval:
pass
if __name__ == '__main__':
g = Interval.mid_err(9.8,.05)
print(repr(g))
g = Interval.min_max(9.75,9.85)
print(repr(g))
d = Interval.mid_err(100,1)
t = (d/(2*g)).sqrt()
print(t,repr(t),t.relative_error())
import driver
driver.default_file_name = 'bsc2.txt'
# driver.default_show_exception=True
# driver.default_show_exception_message=True
# driver.default_show_traceback=True
driver.driver()
Explanation / Answer
Answer:
Note: The given program specification is not clear. So based on the understanding I did it.
When compiling with Python 2.7.12, Im getting an error message “No module named driver”. So I removed that part in the main() and compiled the code.
Code:
from goody import type_as_str
from math import sqrt
class Interval:
def __init__(self, minv, maxv):
self.min=minv
self.max=maxv
@staticmethod
def mid_err( apval, preval):
return Interval(apval-preval, apval+preval)
@staticmethod
def min_max( minv, maxv):
return Interval(minv,maxv)
def __repr__(self):
return "("+str((self.min+self.max)/2)+", +/-"+str((self.max-self.min)/2)+")"
def __rmul__(self,other):
return Interval.min_max(self.min*2 ,self.max*2)
def __div__(self, other):
return Interval.min_max(self.min/other.min, self.max/other.max)
def relative_error(self):
error = (self.max-self.min)
return error *100
def sqrt(self):
return Interval.min_max(sqrt(self.min), sqrt(self.max))
if __name__ == '__main__':
g = Interval.mid_err(9.8,.05)
print(repr(g))
g = Interval.min_max(9.75,9.85)
print(repr(g))
d = Interval.mid_err(100,1)
t = (d/(2*g)).sqrt()
print(t,repr(t),t.relative_error())
Sample Output:
(9.8, +/-0.05)
(9.8, +/-0.05)
((2.25873475935, +/-0.00553191074889), '(2.25873475935, +/-0.00553191074889)', 1.1063821497771187)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.