Python bootcamp · Lab 37

Set intersection

mediumSets12 minLesson: Sets

Read the question, write Python on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read two lines of integers. Print the values that appear on both lines, sorted ascending.

A set intersection is exactly 'values that live in both collections'.

Input. Two lines, each space-separated integers.

Output. The common values, sorted, space-separated. If none, a blank line.

Examples

Example 1
Input
1 2 3 4
3 4 5
Output
3 4
Example 2 — No overlap.
Input
1 2
3 4
Output
Hint
  1. set(a) & set(b) is the intersection.
  2. sorted(...) then print(*...).
Show correct code

Peek only after you have tried. You can still Check your own version.

a = set(map(int, input().split()))
b = set(map(int, input().split()))
print(*sorted(a & b))
main.pyPython · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.