Bruised Apple Detector

EasymathAlso known as Precision, Recall and F1 Score

An orchard's packing line has a camera that flags apples it thinks are bruised. Most apples are fine, so a detector that never flags anything would be right most of the time and still be useless.

Checking a test batch by hand, the orchard wants two honest numbers. The first is the share of flagged apples that were really bruised, and the second is the share of bruised apples that were caught. It also wants one score that balances both. These are the standard scores for any yes/no classifier, from spam filters to medical screening.

Your task

Write detectorScores(flagged, bruised). flagged[i] is the camera's answer for apple i, and bruised[i] is the truth. Count:

  • TP (true positives) are apples that were flagged and bruised.
  • FP (false positives) are apples that were flagged but fine.
  • FN (false negatives) are apples that were not flagged but bruised.

Return [precision, recall, f1] where precision = TP / (TP + FP), recall = TP / (TP + FN), and f1 = 2 · precision · recall / (precision + recall).

Whenever a denominator is 0, that score is 0, so empty lists give [0, 0, 0].

detectorScores(flagged: bool[], bruised: bool[]) → float[] Numbers are accepted within 1e-6.

Examples

Example 1
Input
flagged = [true, true, false, false, true, false]
bruised = [true, false, false, true, true, false]
Output
[0.6666666666666666, 0.6666666666666666, 0.6666666666666666]
Why

Apples 0 and 4 are caught (TP = 2), apple 1 is a false alarm (FP = 1), and apple 3 is missed (FN = 1). Precision is 2 / 3 and recall is 2 / 3, so f1 is also 2 / 3.

Example 2
Input
flagged = [true, true, true, true]
bruised = [true, false, false, false]
Output
[0.25, 1, 0.4]
Why

Flagging everything catches the one bruised apple, so recall is 1. However, only 1 of the 4 flags was right, so precision is 0.25. Then f1 = 2 · 0.25 · 1 / 1.25 = 0.4, which is far below the plain average of 0.625, because f1 drops sharply when either score is poor.

Example 3
Input
flagged = [false, false, false]
bruised = [false, true, false]
Output
[0, 0, 0]
Why

Nothing was flagged, so TP + FP = 0 and precision is 0 by the zero-denominator rule. Recall is 0 / 1 = 0, so f1 is 0 as well.

Constraints

  • 0 ≤ flagged.length = bruised.length ≤ 10,000
  • Each value is true or false.
  • Answers within 0.000001 of the expected values are accepted.
Start with brute force

You could build lists of flagged and bruised positions and intersect them to find TP. That works, but it needs more machinery than necessary.

The more serious trap is using accuracy (correct answers over all apples). When few apples are bruised, a camera that never flags anything scores high on accuracy while catching nothing. Counting the three boxes in one pass gives the honest scores.

Hints

0 of 4 shown

Walkthrough and solution

Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code