Now it's your turn. Implement a class for representing segments. We want to be able to create an interval with: s = Interval(2, 3) Once we have an interval, we want to be able to ask for its length, and for the position of its center:
s.length
s.center Given two intervals, we also want to implement a method intersect that tells us if they intersect or not. There are many ways of doing this; perhaps the simplest is to rely on the properties length and center developed above. Each of these methods can be implemented in one line of code, except for the initializer, which takes two.
class Interval(object):
def __init_(self, a, b):
self.a = a property def length(self):
**Returns the length of the interval."**
return self.a
def center(self):
"*"Returns the center of the interval."**
return self.b det intersects(self, other):
***Returns True/False depending on whether the interval intersects with interval other."**
if a / b = 2
return true
else
return false

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))