We are writing software to analyze logs for toll booths on a highway. This highway is a divided highway with limited access; the only way on to or off of the highway is through a toll booth.
There are three types of toll booths:
ENTRY(Ein the diagram) toll booths, where a car goes through a booth as it enters the highway.EXIT(Xin the diagram) toll booths, where a car goes through a booth as it exits the highway.MAINROAD(Min the diagram), which have sensors that record a license plate as a car drives through at full speed.
We would like to catch people who are driving at unsafe speeds on the highway. To help us do that, we would like to identify journeys where a driver does either of the following:
- Drive an average of 130 km/h or greater in any individual 10 km segment of tollway.
- Drive an average of 120 km/h or greater in any two 10 km segments of tollway.
For example, consider the following journey:
1000.000 TST002 270W ENTRY
1275.000 TST002 260W EXIT
In this case, the driver of TST002 drove 10 km in 275 seconds. We can calculate that this driver drove an average speed of about 130.91 km/h over this segment:
10 km * 3600 sec/hr
-------------------- = 130.91 km/hr
275 sec
Note that:
- A license plate may have multiple journeys in one file, and if they drive at unsafe speeds in both journeys, both should be counted.
- We do not mark speeding if they are not on the highway (i.e. for any driving between an
EXITandENTRYevent). - Speeding is only marked once per journey. For example, if there are 4 segments at 120 km/h or greater, or multiple segments at 130 km/h or greater, the journey is only counted once.
Write a function catch_speeders in LogFile that returns a collection of license plates that drove at unsafe speeds during a journey in the LogFile. If the same license plate drives at unsafe speeds during two different journeys, the license plate should appear twice (once for each journey they drove at unsafe speeds).
For our first task:
- Read through and understand the code and comments below. Feel free to run the code and tests.
- The tests are not passing due to a bug in the code. Make the necessary changes to
LogEntryto fix the bug.
这道题的核心是解析收费站日志并超速车牌。每条日志包含时间戳、车牌、收费站位置与方向、以及事件类型(ENTRY / EXIT / MAINROAD),其中需要先把类似“210E”这样的字段拆成数值位置和方向,再把整份日志按车牌和行程顺序处理,判断车辆是否处于高速公路内的有效行驶区间。题目要求统计两类危险驾驶:单个 10 公里路段平均速度达到 130 km/h,或者任意两个 10 公里路段平均速度达到 120 km/h。实现时通常要按车牌分组,维护一次行程中的路段时间差,并且注意同一车牌可能有多次独立行程,超速要分别计数;而题目给出的 bug 则出在 LogEntry 的解析上,典型做法是把 timestamp 转成浮点数,而不是保留字符串。