Set Union, Intersection, and Difference

Set operations combine membership questions and can express inclusion, overlap, and exclusion without a separate search for every element.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Read three outputs
  2. Choose the direction
  3. Use an overlap that distinguishes all three results
  4. Check empty, disjoint and contained inputs

Read three outputs

For \(A=\{a,b\}\) and \(B=\{b,c\}\), union contains a, b, and c. Intersection contains only b. The difference \(A\setminus B\) contains only a. These use set membership as the underlying property.

Choose the direction

A scheduler can compute requested rooms minus available rooms to find unmet requests. Taking available minus requested answers which rooms remain unused. Verify the direction with a test input in which both groups have an exclusive member.[1]

Use an overlap that distinguishes all three results

For teams \(A=\{2,4,7\}\) and \(B=\{4,8\}\), union contains two, four, seven and eight. Intersection contains just four. Removing B from A leaves two and seven, while removing A from B leaves eight. If these sets are access lists, a rule requiring both conditions uses intersection. A rule requiring access through either uses union. Keep the subtraction direction tied to the question. See subset constraints.

Check empty, disjoint and contained inputs

Two nonempty disjoint inputs have empty intersection. An empty set union any team yields that team unchanged. If B is contained in A, their intersection equals B and their union equals A. These identities give tests without choosing concrete storage order. For quantities such as copies in two warehouses, set union discards frequencies, so decide which model applies before combining. See multiset differences.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩