Write the rule yourself
Show on a big screenThe nearest-example rule is a program you can type.
Loading lab…
- Six examples. A new crate at 3 marked, 2 kilos.
- First example: distance 9. Best so far.
- Second example: distance 4. Smaller. New best.
- Examples 3 to 6: none smaller. Best stays.
- Hand back the winner's word: full price.
The rule
A walk visits the examples one by one, in list order.
Walk the examples once and carry the nearest one so far. Hand back its word at the end. When two are exactly as near, keep the one met first. A vote of several examples reports a tie instead. This walk keeps the first.
- Distance — the gap across, squared, plus the gap up, squared. Across is the marked count. Up is the kilos.
- Best so far — one box for the smallest distance. One box for its word.
Each example is a list: marked count, kilos, word. examples[i] is one example. The second [ ] picks one item from it. examples[i][0] is the marked count. examples[i][2] is that example's word.
Three crates are checked. First: 3 marked, 2 kilos. Second: 10 marked, 6 kilos. Third: 6 marked, 3 kilos. For the third, the nearest example is 5 marked, 2 kilos. Its distance is 1 + 1 = 2. Its word is full price. The real word is cut price, so the walk is wrong there. A fair test reports this miss.
Try the lab to finish this lesson.