6/42
Part 2 of 8 · What an end meansLearn and do
The test that checks where the ends fall misses an interval inside another
There is a second way to write the overlap test that looks just as right. It isn't.
# does an end of b fall inside a?
def an_end_inside(a, b):
return a[0] <= b[0] < a[1] or a[0] < b[1] <= a[1]
print(an_end_inside((3, 5), (1, 7)))
spans = [(s, e) for s in range(8) for e in range(8) if s < e]
pairs = [(a, b) for a in spans for b in spans]
wrong = [(a, b) for a, b in pairs if an_end_inside(a, b) != overlaps(a, b)]
print(len(spans), len(pairs), len(wrong))
print(all(b[0] < a[0] and a[1] < b[1] for a, b in wrong))
an_end_inside(a, b) asks whether an end of b falls inside a. What does an_end_inside((3, 5), (1, 7)) return?
Pick one to see what it prints.
Pick an answer above to see the rest