Experiment Numbers

Source: May 2025, Task 5 (DTU 02002/02003). Study solution — not an official answer key.

Problem

Experiments are strings like 'A-4' (name, dash, positive repetition number).

Write experiment_numbers(experiments) that returns a dict mapping each experiment name to its highest repetition number (as an int).

Example
>>> experiment_numbers(['A-1', 'A-3', 'B-2', 'A-4', 'B-1', 'B-3', 'N-4'])
{'A': 4, 'B': 3, 'N': 4}

Approach

Split each item on '-', track the maximum repetition per name with a dictionary.

Solution

Show solution Hide solution
Solution
def experiment_numbers(experiments):
    result = {}
    for item in experiments:
        name, rep = item.split("-", 1)
        rep = int(rep)
        if name not in result or rep > result[name]:
            result[name] = rep
    return result