"""Tools for booking ice time at a community arena.
This module provides the Booking class and the overlaps() helper.
Times are whole hours on a 24-hour clock (for example, 19 means 7 p.m.).
"""
OPEN_HOUR = 6 # the arena opens at 6 a.m.
CLOSE_HOUR = 23 # and closes at 11 p.m.
def overlaps(start_a, end_a, start_b, end_b):
"""Return True if two time ranges overlap.
Ranges include the start hour and exclude the end hour, so a
booking that ends at 19 does not clash with one that starts at 19.
Args:
start_a: Start hour of the first range.
end_a: End hour of the first range (greater than start_a).
start_b: Start hour of the second range.
end_b: End hour of the second range (greater than start_b).
Returns:
True if the ranges share at least one hour, otherwise False.
>>> overlaps(18, 20, 19, 21)
True
>>> overlaps(18, 19, 19, 20)
False
"""
return start_a < end_b and start_b < end_a
class Booking:
"""One reservation of the ice surface.
Attributes:
team: Name of the team or group.
start: First hour booked.
end: Hour at which the booking ends (not included).
"""
def __init__(self, team: str, start: int, end: int) -> None:
"""Create a booking that fits within opening hours.
Raises:
ValueError: If the booking is outside opening hours or
end is not after start.
"""
if not (OPEN_HOUR <= start < end <= CLOSE_HOUR):
raise ValueError("booking must be within opening hours")
self.team = team
self.start = start
self.end = end
def clashes_with(self, other: "Booking") -> bool:
"""Return True if this booking overlaps the other booking."""
return overlaps(self.start, self.end, other.start, other.end)
if __name__ == "__main__":
# Self-test: run the examples in the docstrings, then a quick demo.
import doctest
print(doctest.testmod())
a = Booking("Bantam A", 18, 20)
b = Booking("Figure skating club", 20, 22)
print(a.clashes_with(b))
print(overlaps.__doc__.splitlines()[0])
arena.py, run directly.