Respuesta :
Answer:
Kindly note that, you're to replace "at" with shift 2 as the brainly text editor can't take the symbol
Explanation:
Copiable code:
# Define the class Interval.
class Interval(object):
# Define the initializer.
def __init__(self, a, b):
# Set the minimum value as a and the maximum
# value as b.
self.a = min(a, b)
self.b = max(a, b)
# Define the function to return the length
# of the interval.
"at"property
def length(self):
return (self.b - self.a)
# Define the function to return the center of
# the interval.
"at"property
def center(self):
# Divide the first and the last value by
# 2 to compute the center.
return (self.a + self.b)/2
# Define the function to check if the two intervals
# intersect or not.
def intersects(self, other):
# Compute the absolute difference between the
# centers of the two intervals and compare it with
# the sum of the half lengths of the intervals
# to check if they intersect or not.
return abs(self.center - other.center) <= (self.length/2 + other.length/2)
The complete program to test the above class is as follows:
Note: No output is generated by the code since all the test cases have been passed successfully.
Code to copy:
# Import the required packages to test the class.
from nose.tools import assert_equal, assert_false, assert_true
# Define the class Interval.
class Interval(object):
# Define the initializer.
def __init__(self, a, b):
# Set the minimum value as a and the maximum
# value as b.
self.a = min(a, b)
self.b = max(a, b)
# Define the function to return the length
# of the interval.
"at"property
def length(self):
return (self.b - self.a)
# Define the function to return the center of
# the interval.
"at"property
def center(self):
# Divide the first and the last value by
# 2 to compute the center.
return (self.a + self.b)/2
# Define the function to check if the two intervals
# intersect or not.
def intersects(self, other):
# Compute the absolute difference between the
# centers of the two intervals and compare it with
# the sum of the half lengths of the intervals
# to check if they intersect or not.
return abs(self.center - other.center) <= (self.length/2 + other.length/2)
# Create different intervals to check the working
# of the above class using assert test cases.
i = Interval(3, 5)
assert_equal(i.length, 2)
i = Interval(3, 5)
assert_equal(i.center, 4)
i = Interval(2, 5)
j = Interval(6, 7)
assert_false(i.intersects(j))
assert_false(j.intersects(i))
k = Interval(4, 6)
assert_true(i.intersects(k))
assert_true(k.intersects(i))
m = Interval(0, 8)
assert_true(i.intersects(m))
assert_true(m.intersects(i))