You are given a highway toll log parser with two classes: LogEntry and LogFile.
Each log line has the format:
timestamp license_plate location booth_type
Example:
44776.619 KTB918 310E MAINROAD
Where:
-
timestamp
is a floating-point number in seconds
-
license_plate
is a string such as
KTB918
-
location
is an integer followed by a direction letter, such as
310E
or
400W
-
booth_type
is one of
ENTRY
,
MAINROAD
, or
EXIT
A journey is defined as:
-
one
ENTRY
-
followed by zero or more
MAINROAD
records
-
followed by one
EXIT
Assume the log contains only complete journeys and no missing records.
Implement or fix the following:
-
Parse log entries correctly
-
In
LogEntry.__init__(log_line)
, parse the timestamp as a numeric value, not a string.
-
Parse the location into:
-
location
: integer part
-
direction
:
EAST
if the suffix is
E
,
WEST
if the suffix is
W
-
Implement
count_journeys()
-
Return the total number of complete journeys in the log.
-
Implement
catch_speeders()
-
Return a collection of license plates for journeys that violate the speed rules.
-
Each pair of consecutive checkpoints within the same journey represents a
10 km
segment.
-
Segment speed is:
speed_kmh = (10 * 3600) / time_diff_seconds
A journey is considered speeding if either of the following is true:
-
any single
10 km
segment has speed at least
130 km/h
, or
-
at least two
10 km
segments in the same journey have speed at least
120 km/h
Additional rules:
-
A plate may appear multiple times in the result if it has multiple speeding journeys.
-
Count each speeding journey only once, even if it contains more than two speeding segments.
-
Do not compute speeds across different journeys for the same car; in particular, do not connect an
EXIT
from one journey to a later
ENTRY
.
You may assume the input order is the log order.