Question
Can someone help me with my Interval code? I don't know why I'm getting this Assertion error. class Interval(object): def __init__(self, x0, x1): # Sorts
Can someone help me with my Interval code? I don't know why I'm getting this Assertion error.
class Interval(object):
def __init__(self, x0, x1):
# Sorts the endpoints, to ensure that x0
x0, x1 = (x0, x1) if x0
assert x0
self.x0 = x0
self.x1 = x1
@property
def length(self):
return self.x1 - self.x0
def endpoints(self):
return (self.x0, self.x1)
def __getitem__(self, i):
"""Alternative way of accessing endpoints."""
if i == 0:
return self.x0
elif i == 1:
return self.x1
raise KeyError()
def __repr__(self):
return "[{},{}]".format(self.x0, self.x1)
### Defining Equality
def interval_equality(self, other):
"""Return True iff the intervals self and other are equal, and False otherwise."""
if not isinstance(other, Interval):
return NotImplemented
return self.x0 == other.x0 and self.x1 == other.x1
Interval.__eq__ = interval_equality
### Interval intersection
def interval_and(self, other):
"""Intersection; returns an interval, or None."""
start = max(self.x0, other.x0)
end = min(self.x1, other.x1)
if(start
return Interval(start, end)
else:
return None
Interval.__and__ = interval_and
### Membership of a point in an interval
def interval_contains(self, x):
if x >= self.x0 and x
return True
return False
Interval.__contains__ = interval_contains
### Interval difference
import copy
def interval_sub(self, other):
"""Subtracts from this interval the interval other, returning a possibly
empty list of intervals."""
other_copy = copy.deepcopy(other)
other_copy.x0 = max(other_copy.x0, self.x0)
other_copy.x1 = min(other_copy.x1, self.x1)
answer = []
if other_copy.x0 > self.x0:
answer.append(Interval(self.x0, other_copy.x0))
if other_copy.x1
answer.append(Interval(other_copy.x1, self.x1))
return answer
Interval.__sub__ = interval_sub
import numpy as np
def total_length(x):
if x is None:
return 0.
elif type(x) == list:
return np.sum([i.length for i in x])
else:
return x.length
print(total_length(None))
i1 = Interval(0, 1)
i2 = Interval(3, 5)
print("i1:", total_length(i1))
print("i2:", total_length(i2))
print("i1+i2:", total_length([i1, i2]))
# 5 points: more tests for interval difference.
import random
def test_random():
i1 = Interval(random.random(), random.random())
i2 = Interval(random.random(), random.random())
d1 = i1 - i2
d2 = i2 - i1
inters = i1 & i2
assert (total_length(d1) + total_length(d2) + 2. * total_length(inters)
== i1.length + i2.length)
for _ in range(100):
test_random()
Question 1: Interval Equality Let us start by implementing equality. We leave this to you. [32] ### Defining Equality def interval_equality(self, other): "Return True iff the intervals self and other are equal, and False otherwise.' if not isinstance(other, Interval): return Not Implemented return self.xo other.xo and self.x1 other.x1 == == Interval. _eq_ interval_equality [33] i Interval(3, 5) j Interval(4, 5) i == False We define union for you, to give you an example. [42] def interval_or(self, other): **** "Union of self and other. Returns a list of 1 or 2 non-overlapping intervals."" if self.x1 = self.xo and x self.xe: answer.append(Interval(self.xe, other_copy.xo)) if other_copy.x1 in
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started