You are given a simplified model for tracking race times on an obstacle course.
-
A
course
has a fixed number of obstacles.
-
A
run
records the time spent on each obstacle in order.
-
A run may be
complete
or
incomplete
.
-
A
run collection
stores many runs for the same course.
For an obstacle index i, the historical times available for that obstacle come from all other runs that reached obstacle i, including incomplete runs.
Implement the method chance_of_personal_best(run) in RunCollection.
Required behavior
Given an in-progress run:
-
Perform
10,000 simulation trials
.
-
In each trial, for every obstacle not yet completed in the current run, randomly choose one historical time for that obstacle from the other runs in the collection that have a recorded time for that obstacle.
-
Combine the current run's existing obstacle times with the sampled remaining times.
-
Check whether the simulated total time is
less than or equal to
the current
personal best
.
-
Return the fraction of trials that are a personal best (or tie the personal best).
Important details
-
The current personal best is the
minimum total time among completed runs only
.
-
When sampling future obstacle times, include times from incomplete historical runs
if they reached that obstacle
.
-
Do
not
sample from the in-progress run being evaluated.
-
The returned value should be a floating-point probability, and with 10,000 trials it should usually be within
0.02
of the true probability.
Example
Suppose a course has 3 obstacles and the historical runs are:
The current in-progress run is [3, 3].
The personal best among completed runs is 8.
For the last obstacle, the historical times are {2, 3}.
So the simulated completed run is either [3,3,2] or [3,3,3], each with equal probability.
Therefore the chance of matching or beating the personal best is about 0.5.
Starter structure
Assume classes similar to the following already exist:
-
Course(title, obstacle_count)
-
Run(course)
with fields:
-
course
-
complete
-
obstacle_times
-
add_obstacle_time(t)
-
get_run_time()
-
RunCollection(course)
with field:
Write the chance_of_personal_best method for RunCollection.