A level · 5 exercises
Python sets
A set holds each value once, in no particular order: set(list) drops repeats at once.
Checking whether a value is in a set is fast. & keeps what two sets share, | joins them, - keeps what is only in the first.
a = set(input().split()) b = set(input().split()) print(sorted(a & b))
Watch for: A set has no order: sort it (sorted()) before printing, or the order may change.
Exercises
- Different wordsHow many different words are in a line?Intermediate
- In both listsPrint the numbers that appear in both lists, sorted, each once.Intermediate
- The missing numberThe numbers from 1 to n are given in some order, but one is missing.Intermediate
- PangramA pangram uses every letter of the English alphabet at least once.Intermediate
- In just one listPrint the numbers that are in exactly one of the two lists, sorted.Intermediate