← Home

062. Set Operations

Mathematical set theory for membership and overlap

062. Set Operations

Every 5 seconds Aryan’s monitor takes a fresh snapshot. He compares the current PIDs against the previous ones using set operations:

prev_pids = {p["pid"] for p in prev_snapshot}  # set comprehension
curr_pids = {p["pid"] for p in curr_snapshot}

# Processes that appeared since last scan
new_pids  = curr_pids - prev_pids
# Processes that terminated since last scan
gone_pids = prev_pids - curr_pids
# Processes that existed in both (stable)
stable    = prev_pids & curr_pids
# All PIDs ever seen this session
all_seen  = prev_pids | curr_pids

if new_pids:
    print(f"New processes:  {new_pids}")
if gone_pids:
    print(f"Terminated:     {gone_pids}")

He also checks whether any watched processes are currently running:

watched = {"malware.exe", "cryptominer", "keylogger"}
running_names = {p["name"] for p in curr_snapshot}

threats = watched & running_names   # intersection
if threats:
    print(f"WARNING: {threats} detected!")

Confusing | and & produces wrong results silently — the hardest kind of bug.


💡 Fun fact: Set operations (, , ) come from Georg Cantor’s set theory, developed in the 1870s and 1880s. Cantor’s work was initially rejected by mathematicians of his era (including Poincaré and Kronecker) but became the foundation of modern mathematics and all of computer science. Every time Aryan writes curr_pids - prev_pids to find new processes, he’s using 150-year-old mathematical theory in production code.

⚠️ Watch out: A & B (intersection) and A | B (union) are easy to mix up because both symbols look “additive.” A simple mnemonic: & is AND — both must be in it; | is OR — either can be in it. Swapping them produces wrong results with no error, because both operations always succeed on any two sets — the bug is purely semantic.

🤔 Think about it: A ^ B (symmetric difference) gives elements in A or B but not both — the “exclusive or” of sets. It equals (A | B) - (A & B). When monitoring processes, what would the symmetric difference of prev_pids and curr_pids represent — and is that a useful metric to track?

Learning objectives

  • Apply & for intersection, | for union, - for difference
  • Understand what each set operation computes
  • Use set operations for membership/overlap queries

Key concepts

  • set operations
  • intersection
  • union
  • difference

Try it

Concept detail

Set operations mirror mathematical set theory:

A & B — intersection: elements in BOTH A and B {“bob”,“charlie”} — friends they share A | B — union: elements in EITHER A or B (or both) {“alice”,“bob”,“charlie”,“diana”} — everyone either knows A - B — difference: elements in A but NOT in B {“alice”} — alice is only in A A ^ B — symmetric difference: in A or B but NOT both {“alice”,“diana”} — each person’s exclusive friends combined

Relational tests: A <= B — subset: every A element is in B A >= B — superset: A contains all of B A.isdisjoint(B) — True if A and B share NO elements

These operations return new sets — originals are not modified. Method equivalents: A.intersection(B), A.union(B), A.difference(B)

Solution

def mutual_friends(a_friends, b_friends):
    return a_friends & b_friends

def all_friends(a_friends, b_friends):
    return a_friends | b_friends

def exclusive_friends(person_friends, other_friends):
    return person_friends - other_friends

def are_connected(a_friends, b_friends):
    return len(a_friends & b_friends) > 0

Tests

A = {"alice", "bob", "charlie"}
B = {"bob", "charlie", "diana"}

def test_mutual_friends():
    assert mutual_friends(A, B) == {"bob", "charlie"}

def test_mutual_friends_none():
    assert mutual_friends({"alice"}, {"diana"}) == set()

def test_all_friends():
    assert all_friends(A, B) == {"alice", "bob", "charlie", "diana"}

def test_exclusive_friends():
    assert exclusive_friends(A, B) == {"alice"}

def test_exclusive_friends_empty():
    # All of A's friends are also in B
    assert exclusive_friends({"bob"}, {"bob", "diana"}) == set()

def test_are_connected_yes():
    assert are_connected(A, B) == True

def test_are_connected_no():
    assert are_connected({"alice"}, {"diana"}) == False

Resources