Intermediate · Sets · 142 of 264
In both lists
Print the numbers that appear in both lists, sorted, each once.
Input: two lines of whole numbers. Output: the common numbers in ascending order, or none
Examples
Input
1 2 3 4 3 4 5 6
Output
3 4
Input
1 2 3 4
Output
none
Input
5 5 1 1 5
Output
1 5
One more test checks other inputs when you press Check.
Stuck? The idea behind 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.