Raw

6. Control questions and tasks / Контрольні запитання і завдання

Understanding of sets

  1. Define a set. What two properties distinguish a set from an arbitrary list, and how does your implementation enforce each of them?
  2. What is the cardinality of a set? What is |\varnothing|?
  3. Why do {1,2,2,3}\{1,2,2,3\} and {1,2,3}\{1,2,3\} denote the same set? Why do {1,2,3}\{1,2,3\} and {3,2,1}\{3,2,1\}?
  4. How do you test whether two sets are equal when they are stored as lists in an arbitrary order?

The operations

  1. Give the formal definition of ABA \cup B, ABA \cap B, ABA \setminus B, and A\overline{A}.
  2. Why does the complement require a universal set UU, while union, intersection, and difference do not?
  3. Express ABA \setminus B using intersection and complement. (Hint: relative to a universe UU, AB=ABA \setminus B = A \cap \overline{B}.)
  4. Which of the operations are commutative? Is AB=BAA \setminus B = B \setminus A in general? Give a counterexample.
  5. State the De Morgan laws for sets: AB=  ?\overline{A \cup B} = \;? and AB=  ?\overline{A \cap B} = \;?

Implementation and complexity

  1. In your implementation, what is the time complexity of containsElement? Of union? How would a hash-based structure change these?
  2. Why must the set operations work regardless of the element type (integers, strings, …)? What does your code rely on to compare elements?
  3. Why should each operation return a new set rather than modify its arguments?

Tasks by hand

  1. Let A={1,2,3,4}A = \{1,2,3,4\}, B={3,4,5}B = \{3,4,5\}, U={1,2,3,4,5,6}U = \{1,2,3,4,5,6\}. Compute ABA \cup B, ABA \cap B, ABA \setminus B, BAB \setminus A, and A\overline{A}.
  2. With A={1,2,3}A = \{1,2,3\}, B={3,4,5}B = \{3,4,5\}, C={5,6,7}C = \{5,6,7\}, evaluate A intersection B union C step by step and confirm the result {3,5,6,7}\{3,5,6,7\} from 4task.md.
  3. For the same sets, evaluate A union B difference C left to right. What is the result?

Laboratory/Laboratory1/6questions.md · 2.0 KB · updated 2026-07-31 21:14