EasyPython

Sets — Set Operations

PythonSetsData Types

Implement solve(a, b, op) where a and b are lists of values and op is one of:

  • "union" → elements in a or b (or both)
  • "intersection" → elements in both a and b
  • "difference" → elements in a but not in b
  • "symmetric_difference" → elements in a or b but not both

Return the result as a sorted list (so the output is deterministic).

Examples

  • solve([1,2,3], [2,3,4], "union")[1, 2, 3, 4]
  • solve([1,2,3], [2,3,4], "intersection")[2, 3]
  • solve([1,2,3], [2,3,4], "difference")[1]
  • solve([1,2,3], [2,3,4], "symmetric_difference")[1, 4]

Sample tests

Test #1Symmetric difference
Input: [[1,2,3],[2,3,4],"symmetric_difference"]
Output: [1,4]
Test #2Union
Input: [[1,2,3],[2,3,4],"union"]
Output: [1,2,3,4]
Test #3Intersection
Input: [[1,2,3],[2,3,4],"intersection"]
Output: [2,3]
Test #4Difference
Input: [[1,2,3],[2,3,4],"difference"]
Output: [1]