Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

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 () 13 14 for in range (100): --- > 15 test_random() 16 in test_random() 1e inters = i1 & i2 11 assert (total_length(d1) + total_length(d2) + 2. * total_length(inters) == i1.length + i2.length) ---> 12 13 14 for in range (100): AssertionError: 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 () 13 14 for in range (100): --- > 15 test_random() 16 in test_random() 1e inters = i1 & i2 11 assert (total_length(d1) + total_length(d2) + 2. * total_length(inters) == i1.length + i2.length) ---> 12 13 14 for in range (100): AssertionError

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

Recommended Textbook for

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions

Question

=+management system of the MNE?

Answered: 1 week ago