Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 20

Find values that appear in exactly one of two sets

Hard

26 minute session

Summary

Return the unique values that are present in one collection but not both, presented in sorted order.

Problem statement

Write a function values_in_exactly_one(a, b) that accepts two iterables (like lists or tuples). Determine which values appear in exactly one of the two inputs (i.e., the symmetric difference). Return the result as a sorted list of unique values. Sorting ensures predictable output for testing. You may assume elements within each input are of a type that can be compared with each other (e.g., all ints or all strings).

Task

Given two iterables, compute the values that appear in exactly one of them and return a sorted list of those unique values.

Examples

Simple symmetric difference

Input

values_in_exactly_one([1, 2, 3], [3, 4])

Output

[1, 2, 4]

Explanation

1 and 2 are only in the first; 4 is only in the second.

Input format

Two iterables passed as arguments: values_in_exactly_one(a, b).

Output format

Return a sorted list containing values that appear in exactly one of the inputs.

Constraints

- Elements within each input are comparable (so sorted() works). - Inputs may contain duplicates; the result should contain each unique value once. - Do not use any external libraries.

Samples

Sample input 0

values_in_exactly_one(['a', 'b', 'b'], ['b', 'c'])

Sample output 0

['a', 'c']

Explanation 0

Unique values only in one side are 'a' and 'c', returned sorted.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

09:15 PM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.