interactive-coding-challenges/online_judges/busiest_period/busiest_period_challenge.ipynb

237 lines
6.0 KiB
Python
Raw Normal View History

2017-03-29 16:35:42 +08:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Given an array of (unix_timestamp, num_people, EventType.ENTER or EventType.EXIT), find the busiest period.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)\n",
"* [Solution Notebook](#Solution-Notebook)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
"* Can we assume the input array is valid?\n",
" * Check for None\n",
"* Can we assume the elements of the input array are valid?\n",
" * Yes\n",
"* Is the input sorted by time?\n",
" * No\n",
"* Can you have enter and exit elements for the same timestamp?\n",
" * Yes you can, order of enter and exit is not guaranteed\n",
"* Could we have multiple enter events (or multiple exit events) for the same timestamp?\n",
" * No\n",
"* What is the format of the output?\n",
" * An array of timestamps [t1, t2]\n",
"* Can we assume the starting number of people is zero?\n",
" * Yes\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* None -> TypeError\n",
"* [] -> None\n",
"* General case\n",
"\n",
"<pre>\n",
"timestamp num_people event_type\n",
"1 2 EventType.ENTER\n",
"3 1 EventType.ENTER\n",
"3 2 EventType.EXIT\n",
"7 3 EventType.ENTER\n",
"8 2 EventType.EXIT\n",
"9 2 EventType.EXIT\n",
"\n",
"result = Period(7, 8)\n",
"</pre>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from enum import Enum\n",
"\n",
"\n",
"class Data(object):\n",
"\n",
" def __init__(self, timestamp, num_people, event_type):\n",
" self.timestamp = timestamp\n",
" self.num_people = num_people\n",
" self.event_type = event_type\n",
"\n",
" def __lt__(self, other):\n",
" return self.timestamp < other.timestamp\n",
"\n",
"\n",
"class Period(object):\n",
"\n",
" def __init__(self, start, end):\n",
" self.start = start\n",
" self.end = end\n",
"\n",
" def __eq__(self, other):\n",
" return self.start == other.start and self.end == other.end\n",
"\n",
" def __repr__(self):\n",
" return str(self.start) + ', ' + str(self.end)\n",
"\n",
"\n",
"class EventType(Enum):\n",
"\n",
" ENTER = 0\n",
" EXIT = 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def find_busiest_period(self, data):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# %load test_find_busiest_period.py\n",
"from nose.tools import assert_equal, assert_raises\n",
"\n",
"\n",
"class TestSolution(object):\n",
"\n",
" def test_find_busiest_period(self):\n",
" solution = Solution()\n",
" assert_raises(TypeError, solution.find_busiest_period, None)\n",
" assert_equal(solution.find_busiest_period([]), None)\n",
" data = [\n",
" Data(3, 2, EventType.EXIT),\n",
" Data(1, 2, EventType.ENTER),\n",
" Data(3, 1, EventType.ENTER),\n",
" Data(7, 3, EventType.ENTER),\n",
" Data(9, 2, EventType.EXIT),\n",
" Data(8, 2, EventType.EXIT),\n",
" ]\n",
" assert_equal(solution.find_busiest_period(data), Period(7, 8))\n",
" print('Success: test_find_busiest_period')\n",
"\n",
"\n",
"def main():\n",
" test = TestSolution()\n",
" test.test_find_busiest_period()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}