bhylu.com

Advertisements

Set Exercises

Find attendees who appear in both sessions, and those who appear only in the morning. Use set operations rather than nested search loops.

Understand the idea

Write your own solution before opening the worked answer. Predict the output from the supplied values, then compare the actual result. Check both an ordinary case and the boundary described below; a passing example is more useful when you can explain why it passes.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

Your task

Find attendees who appear in both sessions, and those who appear only in the morning. Use set operations rather than nested search loops.

Show the worked solution
Python

morning = {"Asha", "Dev"}
evening = {"Dev", "Noor"}
print(sorted(morning & evening))
print(sorted(morning - evening))

Check the result

Dev is shared, while Asha is morning-only.

Test one more case

Find people attending exactly one session and explain the symmetric difference.