This notebook was prepared by Donne Martin. Source and license info is on GitHub.
timestamp num_people event_type 1 2 EventType.ENTER 3 1 EventType.ENTER 3 2 EventType.EXIT 7 3 EventType.ENTER 8 2 EventType.EXIT 9 2 EventType.EXIT result = Period(7, 8)
Refer to the Solution Notebook. If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start.
from enum import Enum
class Data(object):
def __init__(self, timestamp, num_people, event_type):
self.timestamp = timestamp
self.num_people = num_people
self.event_type = event_type
def __lt__(self, other):
return self.timestamp < other.timestamp
class Period(object):
def __init__(self, start, end):
self.start = start
self.end = end
def __eq__(self, other):
return self.start == other.start and self.end == other.end
def __repr__(self):
return str(self.start) + ', ' + str(self.end)
class EventType(Enum):
ENTER = 0
EXIT = 1
class Solution(object):
def find_busiest_period(self, data):
# TODO: Implement me
pass
The following unit test is expected to fail until you solve the challenge.
# %load test_find_busiest_period.py
import unittest
class TestSolution(unittest.TestCase):
def test_find_busiest_period(self):
solution = Solution()
self.assertRaises(TypeError, solution.find_busiest_period, None)
self.assertEqual(solution.find_busiest_period([]), None)
data = [
Data(3, 2, EventType.EXIT),
Data(1, 2, EventType.ENTER),
Data(3, 1, EventType.ENTER),
Data(7, 3, EventType.ENTER),
Data(9, 2, EventType.EXIT),
Data(8, 2, EventType.EXIT),
]
self.assertEqual(solution.find_busiest_period(data), Period(7, 8))
print('Success: test_find_busiest_period')
def main():
test = TestSolution()
test.test_find_busiest_period()
if __name__ == '__main__':
main()
Review the Solution Notebook for a discussion on algorithms and code solutions.