Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
clases y funciones
class Interval:
start = 0
end = 0
def __init__(self, a, b):
self.start = a
self.end = b
if b < a:
self.start = b
self.end = a
def len(self) :
return self.end - self.start
def display(self):
print "start:{} end:{}".format(self.start, self.end)
def is_in(self, value) :
if value < self.end and value > self.start:
return True
else:
return False
def union(self, other) :
return Interval(min(self.start, other.start), max(self.end, other.end))
def collide(self, other) :
if self.end < other.start:
return False
if other.end < self.start:
return False
return True
def move(self, offset) :
self.start += offset
self.end += offset
int1 = Interval(5, 1)
int2 = Interval(4, 10)
int3 = int1.union(int2)
int3.display()
print int1.collide(int2)
print Interval(1,2).collide(Interval(5, 10))
print int1.union(int2).collide(Interval(10, 3))
int1.move(10)
int1.display()
int1 = Interval(5, 1)
int1.display()
int1.start = 1000
int1.display()
interval1 = [5 , 2]
interval2 = [5, 10]
interval3 = [20, 34]
def largo_intervalo(interval):
if interval[0] > interval[1] :
largo = interval[0] - interval[1]
else:
largo = interval[1] - interval[0]
return largo
def print_intervalo(interval) :
print "start:{} end:{} len:{}".format(interval[0], interval[1], largo_intervalo(interval))
def is_in_intervalo(interval, test):
start = interval[0]
end = interval[1]
if interval[0] > interval[1] :
start = interval[1]
end = interval[0]
if test < end and test > start:
return True
else:
return False
test = is_in_intervalo(interval1, 2.5)
print test
print_intervalo(interval2)
print largo_intervalo(interval1) + largo_intervalo(interval2) + largo_intervalo(interval3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.