Skip to content

Instantly share code, notes, and snippets.

@amcgregor
Created January 2, 2020 19:55
Show Gist options
  • Save amcgregor/6839a068bb908565339853618c1372da to your computer and use it in GitHub Desktop.
Save amcgregor/6839a068bb908565339853618c1372da to your computer and use it in GitHub Desktop.
A demonstration I use of an alternate use for a sorting algorithm.
from bisect import bisect_left
periods = [
# Time, is_open
('00:00', False),
('08:30', True),
('12:00', False),
('13:15', True),
('17:30', False),
('24:00', False), # For completeness sake.
]
def is_open(time):
applicable_period = bisect_left(periods, (time, None)) - 1
return periods[applicable_period][1]
print(is_open('14:22')) # True # Pulled from the '13:15' "record".
print(is_open('06:44')) # False # Pulled from the '00:00' "record".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment