Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Python object oriented programming, implement a class calledIntervalthat represents a one-dimensional open interval on the real line. This main purpose of this class is

Using Python object oriented programming, implement a class calledIntervalthat represents a one-dimensional open interval on the real line. This main purpose of this class is to simplify overlapping continuous intervals. The code below should get you started but there are a lot of missing pieces that you will have to figure out.

The API should take a pair of integers as input and respond to the+operator such that

 >>> a = Interval(1,3) >>> b = Interval(2,4) >>> c = Interval(5,10) >>> a + b Interval(1,4) >>> b+c [ Interval(2,4), Interval(5,10)] 
  • Note that in the case of non-overlapping intervals, the output should be a list of constituentIntervals. Keep in mind that these areopenintervals. Specifically,
>>> Interval(2,3)+Interval(1,2) [Interval(2,3), Interval(1,2)] 
  • Note that these do not produce a single interval because each interval is open (not closed). The interval endpoints can be negative also (e.g.,Interval(-10,-3)is valid). The outputdoes nothave to be sorted.
  • It's up to you to write the dunder functions for your object. If you do this right, you will have a very general solution to this problem.

Note: Make sure to implement__eq__method below, to pass all the test cases in the grader.

Starter Code:

 # fill out the necessary methods shown below and add others if need be. class Interval(object): def __init__(self,a,b): """ :a: integer :b: integer """ assert aassert isinstance(a,int) assert isinstance(b,int) self._a = a self._b = b def __repr__(self): pass def __eq__(self,other): pass def __lt__(self,other): pass def __gt__(self,other): pass def __ge__(self,other): pass def __le__(self,other): pass def __add__(self,other): pass 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions