Course 1.2Lesson 5 of 5
In this project we manage the clubs of a school. The members of each club are a set, and the clubs are kept in a dictionary. With the set operations the program finds all the students, the students who are in every club, the students who are only in one club, how many clubs each student joins, and the overlap of each pair of clubs. We plan the questions and work out the expected answers by hand, build the program in steps, test the unusual cases such as an empty club, and extend it with a club that members can join and leave.
Try the code and the simulations beside the lesson.
After this lesson you can
A school has three clubs: cricket, chess and music. The principal wants to know
The members of each club are a set (nobody is a member twice, and the order does not matter). The clubs are in a dictionary, with the club name as the key.
| Club | Members |
|---|---|
| cricket | Asha, Ravi, Meera, Dev |
| chess | Ravi, Kabir, Meera |
| music | Asha, Kabir, Ana, Meera |
Work out the answers by hand.
Change the data, and work out the answers by hand first.
| Test | Expected |
|---|---|
| one club only | everyone is in every club, and the "only in" answer is all |
| an empty club | no one is in every club, and the union is not changed |
| a student in a single club three times in the data | still only once, because a set |
| two clubs with the same members | the difference is empty |
The second row is the edge. Does the program still work if a club is set()? The intersection with an empty set is empty, which is the right answer: no one belongs to every club.
For each pair, the project finds the members of both clubs with &.
Pick one or two, and do them without looking at a solution. Write the expected answers first.
join(clubs, club, student) and leave(clubs, club, student). Use add and discard.^ to find the students who are in exactly one of two clubs.in_all = everyone is a second name for the same set. Use set(everyone) or everyone.copy().sorted.i + 1.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
set(everyone) when we start the intersection?i + 1?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.