Skip to content

Sets — solutions

Solutions to the practice problems at the end of Sets.

  1. {0,3,6,9}\{0, 3, 6, 9\}. The multiples of three among {0,1,2,…,10}\{0, 1, 2, \ldots, 10\}.
  2. {0,2,6,12,20}\{0, 2, 6, 12, 20\}. The values of n2−nn^2 - n for n=1,2,3,4,5n = 1, 2, 3, 4, 5.
  3. {(1,2),(2,1)}\{(1, 2), (2, 1)\}. The pairs with a≠ba \neq b out of the four pairs in {1,2}×{1,2}\{1, 2\} \times \{1, 2\}.
  1. A∪B={1,2,3,4,5,6,7}A \cup B = \{1, 2, 3, 4, 5, 6, 7\}.
  2. A∩B={4,5}A \cap B = \{4, 5\}.
  3. A∖B={1,2,3}A \setminus B = \{1, 2, 3\}.
  4. (A∪B)∩C={1,2,3,4,5,6,7}∩{2,4,6,8}={2,4,6}(A \cup B) \cap C = \{1, 2, 3, 4, 5, 6, 7\} \cap \{2, 4, 6, 8\} = \{2, 4, 6\}.
  5. A∩B∩C={4,5}∩{2,4,6,8}={4}A \cap B \cap C = \{4, 5\} \cap \{2, 4, 6, 8\} = \{4\}.
  6. By inclusion–exclusion with ∣A∣=5|A| = 5, ∣B∣=4|B| = 4, ∣C∣=4|C| = 4, ∣A∩B∣=2|A \cap B| = 2, ∣A∩C∣=2|A \cap C| = 2, ∣B∩C∣=2|B \cap C| = 2, and ∣A∩B∩C∣=1|A \cap B \cap C| = 1, we get ∣A∪B∪C∣=5+4+4−2−2−2+1=8|A \cup B \cup C| = 5 + 4 + 4 - 2 - 2 - 2 + 1 = 8. The union is {1,2,3,4,5,6,7,8}\{1, 2, 3, 4, 5, 6, 7, 8\}, which checks out.

Let MM be the set of students taking math and CC the set taking computer science. We have ∣M∣=35|M| = 35, ∣C∣=28|C| = 28, and ∣M∩C∣=15|M \cap C| = 15. Then

∣M∪C∣=∣M∣+∣C∣−∣M∩C∣=35+28−15=48.|M \cup C| = |M| + |C| - |M \cap C| = 35 + 28 - 15 = 48.

So 48 students take at least one of the two courses, and 60−48=1260 - 48 = 12 take neither.

The eight subsets of {a,b,c}\{a, b, c\} are ∅\emptyset, {a}\{a\}, {b}\{b\}, {c}\{c\}, {a,b}\{a, b\}, {a,c}\{a, c\}, {b,c}\{b, c\}, and {a,b,c}\{a, b, c\}. In general, a set with nn elements has 2n2^n subsets.

Four of these contain aa: {a}\{a\}, {a,b}\{a, b\}, {a,c}\{a, c\}, and {a,b,c}\{a, b, c\}. In general, exactly half of the subsets of a nonempty set contain any one chosen element, because fixing that element to be “in” or “out” splits the subsets into two equal classes.

We show that (A∪B)∖C⊆(A∖C)∪(B∖C)(A \cup B) \setminus C \subseteq (A \setminus C) \cup (B \setminus C) and vice versa.

Forward direction. Let x∈(A∪B)∖Cx \in (A \cup B) \setminus C. Then x∈A∪Bx \in A \cup B and x∉Cx \notin C, so x∈Ax \in A or x∈Bx \in B, and in either case x∉Cx \notin C. If x∈Ax \in A, then x∈A∖Cx \in A \setminus C; if x∈Bx \in B, then x∈B∖Cx \in B \setminus C. Either way, x∈(A∖C)∪(B∖C)x \in (A \setminus C) \cup (B \setminus C).

Reverse direction. Let x∈(A∖C)∪(B∖C)x \in (A \setminus C) \cup (B \setminus C). Then x∈A∖Cx \in A \setminus C or x∈B∖Cx \in B \setminus C. In the first case, x∈Ax \in A and x∉Cx \notin C, so x∈A∪Bx \in A \cup B and x∉Cx \notin C, so x∈(A∪B)∖Cx \in (A \cup B) \setminus C. The second case is symmetric.

Both inclusions hold, so the two sets are equal.

def common_letters(s, t):
return {c.lower() for c in s if c.isalpha()} & {c.lower() for c in t if c.isalpha()}

The two comprehensions build sets of lowercased letters from each string, and & intersects them.

The word sets are A={a, rose, is}A = \{\text{a, rose, is}\} and B={roses, are, red, blue}B = \{\text{roses, are, red, blue}\}. The intersection is ∅\emptyset (no word appears in both), and the union has seven distinct words, so

J(A,B)=∣A∩B∣∣A∪B∣=07=0.J(A, B) = \frac{|A \cap B|}{|A \cup B|} = \frac{0}{7} = 0.

The score is zero even though the two sentences are intuitively related: “rose” and “roses” count as different words under this tokenization. Reducing words to a common root (stemming or lemmatization) before computing the sets would raise the score.