──────────────────────────────────────

16. Festival Workshop Shortlist · Build a Festival Workshop Shortlist

Build one ranking function with named options, predictable ties and a configurable result count.

Lesson files · lessons/callables/festival-shortlist-project

Before you start
- Select a sort key and break ties with a tuple.
- Pass a function to another function.
- Use defaults and pass optional settings by name.

By the end
- Rank workshops by either duration or seat count with alphabetical ties.
- Check the shortlist with the default limit and with a limit of zero.
- Explain which settings the starter passes when the organiser chooses default.

Where you can use it
Let an organiser ask for the three workshops with the most seats or the two shortest sessions without changing the workshop records.

Choose one key callback for each ranking mode. sorted calls the selected key for each record. A tuple key compares its first value, then uses the second to break a tie. Compare minutes directly for shortest first and negate the seat count for most seats first. Keep names as the second value in both keys. A slice like ranked[:limit] keeps at most limit rows. The starter reads the settings separately and uses **options to pass only the settings the organiser changed.

Predict
In duration order, which comes first, Music or Tables?
- Tables.
- Music.

Worked example (example.py)

def order_sessions(sessions, order="name"):
    keys = {
        "name": lambda session: session[0],
        "duration": lambda session: (session[1], session[0]),
    }
    return sorted(sessions, key=keys[order])


sessions = [("Welcome", 30), ("Music", 10), ("Tables", 10)]
options = {"order": "duration"}
for name, minutes in order_sessions(sessions, **options):
    print(name)


Example result
Music
Tables
Welcome

Complete shortlist(workshops, order="shortest", limit=2) for the supplied workshops. Each row holds a name, duration in minutes and available seats. The first input line is shortest, seats or default. The second is a non-negative limit or default. Rank shortest workshops first in shortest mode and workshops with the most seats first in seats mode. Break ties alphabetically and return at most limit rows. Keep the starter’s display and invalid-option handling.

Starter program (starter.py)

workshops = [
    ("Robots", 35, 12),
    ("Printmaking", 20, 8),
    ("Bookbinding", 20, 12),
    ("Puppets", 30, 16),
]


def shortlist(workshops, order="shortest", limit=2):
    return workshops


def read_options():
    order = input().strip()
    limit = input().strip()
    options = {}
    if order != "default":
        if order not in ("shortest", "seats"):
            raise ValueError("Unknown order")
        options["order"] = order
    if limit != "default":
        count = int(limit)
        if count < 0:
            raise ValueError("Negative limit")
        options["limit"] = count
    return options


try:
    options = read_options()
except ValueError:
    print("Invalid options.")
else:
    selected = shortlist(workshops, **options)
    if not selected:
        print("No workshops selected.")
    for name, minutes, seats in selected:
        print(f"{name} | {minutes} min | {seats} seats")


Starter input
seats
3

Hint
Make two key functions and pass the selected one to sorted. Slice the ranked list after sorting.

Case 1. Try the sample
Input
seats
3
Expected output
Puppets | 30 min | 16 seats
Bookbinding | 20 min | 12 seats
Robots | 35 min | 12 seats

Case 2. Use both defaults
Input
default
default
Expected output
Bookbinding | 20 min | 12 seats
Printmaking | 20 min | 8 seats

Case 3. Select zero workshops
Input
seats
0
Expected output
No workshops selected.

Project checklist
Run the sample case.

Project checklist
Try one different case and explain what happened.

Marking criterion
Program behaviour (4 points)
Each required behaviour works on the supplied cases.

Marking criterion
Use of the lesson concept (3 points)
The code uses the main idea in a clear, purposeful way.

Marking criterion
Test evidence (2 points)
The reflection describes a case, its expected result and what happened.

Marking criterion
Explanation (1 points)
The learner explains a change or design decision in their own words.

Stretch
Add a name mode and try a limit larger than the number of workshops.

Reflect
Which test would fail if the sort key counted seats in ascending order?

Check your prediction
Music. The durations match, so the names decide the order.
