# Exercise: Fruit Sorter

Write four functions for Three Rivers Fruit Packers, a straight-line distance and a nearest-neighbors search in plain Python, then a decision tree and a nearest neighbors classifier with the settings you choose, and let pytest check the distances, the neighbors' order and the models' settings.

Journeyman · AI Basics · Python · Updated November 19, 2026

By Gregor Dzierzon

Apples, pears and plums roll down one belt at Three Rivers Fruit Packers, and the packers want a machine that sorts them. You'll write the four functions behind it: two in plain Python that measure how far apart two fruits are and find the nearest ones, the idea inside nearest neighbors, and two that make the chapter's two models with the settings you pick. Tests call each function and check what it gives back, and a notebook comes with the exercise for looking at the fruit, and at your tree, while you work.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-ml-fruit-sorter](https://github.com/ask-codey/exercise-ml-fruit-sorter), also linked from the **Exercise** box beside the lesson. Clone it into your `python-course` folder. In VS Code's terminal:

```bash
cd ~/python-course
git clone https://github.com/ask-codey/exercise-ml-fruit-sorter.git
```

Git makes a folder, `exercise-ml-fruit-sorter`, with the exercise code inside. Open that folder in VS Code (**File > Open Folder**), open its terminal with **Terminal > New Terminal**, and make the exercise's virtual environment. On Windows (PowerShell):

```powershell
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
```

On a Mac (zsh):

```bash
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

The prompt starts with `(.venv)` once the virtual environment is on. The exercise's README says what each command does, and what to do if PowerShell won't run the second one.

The folder holds:

- `fruit.csv`: the 450 fruits, our own made-up data, the same file the chapter's lessons use.
- `seed_data.py`: the script that made the CSV, with the pattern it planted. You don't need to run it: the committed CSV is what keeps everyone's numbers the same.
- `fruit_sorter.py`: the four functions, for you to finish, with `FEATURES` (the columns the models look at: `weight_g`, `diameter_cm`, `sugar_brix` and `skin_redness`) and `LABEL` (`fruit`), ready to use.
- `explore.ipynb`: a notebook for looking at the fruit and trying your code, with a cell that draws your tree. Choose the `.venv` kernel with **Select Kernel** (top right) before you run it. The tests don't read it.
- `report.py`: splits the fruit, measures a distance, finds the new fruit's neighbors and trains both models with your four functions, and prints what each step gave. Ready to use.
- `tests/`: the tests, which call each function and check what comes back, and `conftest.py`, which gives each test a fresh copy of the fruit. You don't need to change them.
- `requirements.txt` and `pytest.ini`: the packages the exercise needs, at exact versions, and where pytest finds the tests and your code.

## What to do

Every function in `fruit_sorter.py` is already declared with its name and parameters, and for now it stops with a `NotImplementedError` that says *Not built yet.* The docstring under each one says what it should do, with examples:

```python
def nearest_labels(examples, point, k):
    """Return the labels of the k examples nearest to point, nearest first, as a list.

    examples is a list of (measurements, label) pairs, such as
        [((0, 0), "pear"), ((5, 0), "apple"), ((1, 1), "plum")]
    and point is the measurements of one new example, such as (0, 0).
    Measure each example's distance to point with distance().

    Examples at the same distance keep the order they have in examples.
    If k is more than the number of examples, return every example's label.

    With the three examples above:
    nearest_labels(examples, (0, 0), 2) -> ["pear", "plum"]
    nearest_labels(examples, (4, 0), 1) -> ["apple"]
    """
    raise NotImplementedError("Not built yet.")
```

In each function, replace the `raise NotImplementedError("Not built yet.")` line with your own code:

1. **`distance(a, b)`** returns the straight-line distance between two points of any number of features, as *Distance Between Examples* works it out: `5.0` for `(0, 0)` and `(3, 4)`, `2.0` for `(1, 1, 1, 1)` and `(2, 2, 2, 2)`. Plain Python; `math.dist` is fine.
2. **`nearest_labels(examples, point, k)`** measures each example's distance to `point` with your `distance`, and returns the labels of the `k` nearest, nearest first, as a list. With the three examples above and the point `(0, 0)`: the pear is 0 away, the plum about 1.41 and the apple 5, so `k = 2` gives `["pear", "plum"]`. Equally near examples keep the order they came in; a `k` bigger than the list gives every label.
3. **`build_tree(max_depth, seed)`** returns a new `DecisionTreeClassifier`, not fitted, with `max_depth` and `seed` passed on as `random_state`, every other setting left at its default, as in *Decision Trees*.
4. **`build_neighbors(k)`** returns a new `KNeighborsClassifier`, not fitted, that lets `k` neighbors vote, as in *Nearest Neighbors*.

`distance` and `nearest_labels` are plain Python on purpose: they're the whole idea inside `KNeighborsClassifier`, and one test checks that your `nearest_labels` finds the same five training fruits that scikit-learn does.

## Check your work

Run the tests from the exercise folder, with the virtual environment on:

```bash
python -m pytest
```

At first all 19 tests fail. That's expected: each function you finish turns its tests green. A failing test's message says what it called, what it expected and what came back, for example *nearest_labels(EXAMPLES, (0, 0), 2) should return ['pear', 'plum'], but it returned ['plum', 'pear'].* No test checks how many fruits a model gets right, which depends on the data: the tests check the distances, the order of the neighbors and the models' settings.

To see your functions at work on the whole file, run `python report.py`.

## Hints

- **Look before you write.** Run the notebook's first cells: `fruit.head(10)` and `fruit.describe()` show the columns and their ranges, and how much bigger the weights are than everything else.
- **Pythagoras in any number of directions.** Square each pair's difference, add them up, take the square root, or let `math.dist(a, b)` do all three.
- **Sort, then slice.** `sorted(examples, key=...)` with a `key` that works out an example's distance to `point` puts the examples in order, nearest first. A `key` is a small function that `sorted` calls on each item and sorts by its answer; `lambda example: distance(example[0], point)` writes one in a line (`lambda`, then the item's name, a colon, and what to give back). `sorted` keeps equally near examples in the order they came in. `[:k]` keeps the first `k`, even when there are fewer.
- **Make, don't fit.** `build_tree` and `build_neighbors` each need one line: make the model with its settings and return it. `report.py` and the tests call `fit`.

## When you're done

Every test is green: pytest's last line says `19 passed`. And `python report.py` prints:

```text
1) fruits: 450
2) training fruits: 360
3) test fruits: 90
4) distance from the new fruit to the first training fruit: 10.06
5) the new fruit's 5 nearest training fruits: ['apple', 'apple', 'apple', 'apple', 'apple']
6) tree, max_depth 3, test fruits right: 82 of 90
7) nearest neighbors, k = 5, test fruits right: 78 of 90
8) the tree's guess for the new fruit: apple
9) the neighbors' guess for the new fruit: apple
```

Lines 6 and 7 count right answers on 90 test fruits; *Accuracy*, in the next chapter, turns that count into a score. Then explore in the notebook: draw your tree, then try `build_tree(1, 0)` and `build_neighbors(1)`. Which fruits does each get wrong? And print the new fruit's five nearest training fruits with their weights: what do you notice?

## Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. *Hand In Your Work with Git* walks through each step.

1. On GitHub, click **+**, then **New repository**. Name it (for example `exercise-ml-fruit-sorter`), choose **Public**, and click **Create repository**. Leave out the README: your folder already has one.
2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

   ```bash
   git remote set-url origin https://github.com/your-name/exercise-ml-fruit-sorter.git
   git add .
   git commit -m "My solution"
   git push -u origin main
   ```

3. Paste your repo's address, such as `https://github.com/your-name/exercise-ml-fruit-sorter`, into **Submit your exercise** on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

## So what

A distance, a sorted list and two models built with settings you chose: that's the inside of nearest neighbors and the outside of a decision tree, each small enough to test.

## Key terms

- [Euclidean Distance](https://www.ask-codey.com/glossary/euclidean-distance)
- [k-Nearest Neighbors](https://www.ask-codey.com/glossary/k-nearest-neighbors)
- [Decision Tree](https://www.ask-codey.com/glossary/decision-tree)
- [pytest](https://www.ask-codey.com/glossary/pytest)

## Related

- [Decision Trees](https://www.ask-codey.com/lessons/ml-decision-trees)
- [Reading a Decision Tree](https://www.ask-codey.com/lessons/ml-reading-a-decision-tree)
- [Distance Between Examples](https://www.ask-codey.com/lessons/ml-distance-between-examples)
- [Nearest Neighbors](https://www.ask-codey.com/lessons/ml-nearest-neighbors)

Source: https://www.ask-codey.com/lessons/exercise-ml-fruit-sorter
