Values in both lists

Read two lists of whole numbers and print the values that appear in both, in increasing order.

What to build

A program that combines two sets.

Requirements

  • Read the two lists.
  • Print the values in both, in increasing order, or NONE.

Input

Two lines, each with whole numbers separated by spaces.

Output

The common values in increasing order, separated by spaces, or NONE if there are none.

Constraints

Each line has at most 1000 whole numbers.

Example

Input:

1 2 3 4
3 4 5

Output:

3 4

Why it works

& gives the values in both sets directly. Sets have no order, so sorted is needed before printing.

Examples

Example 1 · Input
1 2 3 4
3 4 5
Expected output
3 4
Example 2 · Input
1 2
3 4
Expected output
NONE

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Convert each value with int() so that 10 sorts after 9.

Hint 2

a & b keeps the values in both sets; sorted() puts them in order.

Solution

Show one correct solution (try it yourself first)
a = {int(x) for x in input().split()}
b = {int(x) for x in input().split()}
common = sorted(a & b)
print(" ".join(str(x) for x in common) if common else "NONE")

Lesson: Tuples and sets →