This is the training step of the lesson's first program, for any data: find the cut-off that makes the fewest mistakes.
What to build
A program that tries every sensible cut-off and reports the best rule and its score.
Requirements
- The rule predicts
1when value ≥ cut-off. - Try each different value as the cut-off, in increasing order, and then the largest value plus 1.
- If several cut-offs are equally good, choose the smallest.
- Print
pass if value >= C, thenR of N correct.
Input
Line 1: the values (integers). Line 2: their labels, 0 or 1.
Output
Two lines.
Constraints
1 to 1000 values between −10⁶ and 10⁶.
Example
Input:
1 2 3 4 5 6 7 8
0 0 0 1 0 1 1 1
Output:
pass if value >= 4
7 of 8 correct
Why it works
Only the data's own values can change which examples a rule calls positive, so trying each of them (and one rule that rejects everything) covers every different rule.
Examples
1 2 3 4 5 6 7 8 0 0 0 1 0 1 1 1
pass if value >= 4 7 of 8 correct
3 1 2 1 0 1
pass if value >= 2 3 of 3 correct
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The candidates are the different values, smallest first, and then the largest value plus 1 (a rule that never says 1).
Hint 2
Count the right predictions for every candidate and keep a candidate only if it is strictly better than the best so far.
Solution
Show one correct solution (try it yourself first)
values = list(map(int, input().split()))
labels = list(map(int, input().split()))
best_cutoff, best_correct = None, -1
for cutoff in sorted(set(values)) + [max(values) + 1]:
correct = sum(1 for v, y in zip(values, labels) if (v >= cutoff) == (y == 1))
if correct > best_correct:
best_cutoff, best_correct = cutoff, correct
print(f"pass if value >= {best_cutoff}")
print(f"{best_correct} of {len(values)} correct")