2. Methodical guidelines / Методичні вказівки
This section is self-contained: it collects all the theory of the work’s theme — finite sets and their operations — together with practical guidance for carrying out the tasks in 4task.md. No outside material is required.
2.1 Sets: basic notions
A set is an unordered collection of distinct objects, called its elements. We write if is an element of , and otherwise. A finite set may be written by listing its elements in braces, e.g. .

Two conventions define what a set is:
- Distinctness (no duplicates). An element is either in the set or not; repeating it changes nothing: .
- No order. The elements have no positions: .
Equality. means they have exactly the same elements — every element of is in and every element of is in . Equality is therefore decided by membership, not by position. This is the rule you must use to compare two sets that are stored as lists in an arbitrary order.
Cardinality. is the number of distinct elements of . The empty set contains nothing, so .
Subset. (“A is a subset of B”) means every element of is also in . Note exactly when and .
Universal set. Many problems fix in advance a set of all elements under consideration — the universe. It is the reference domain against which the complement of a set is taken (see below).
2.2 The operations of this work
Let and be sets and a universal set with .
| Operation | Notation | Definition | Example (, ) |
|---|---|---|---|
| Union | |||
| Intersection | |||
| Difference | |||
| Complement | if |

Two remarks that matter for the implementation:
- Complement is relative. has no meaning without a universe;
outside there is no notion of “everything not in .” Your
complementfunction therefore takes the universal set as an explicit argument. - Difference is directed. and are different in general: for the sets above, but . Sets with are called disjoint.
2.3 The algebra of sets
The operations obey algebraic laws. You do not need to prove them for this work, but they explain why your functions should behave as they do, and they let you check results by hand (see 6questions.md).
| Law | Statement |
|---|---|
| Commutativity | , |
| Associativity | , and likewise for |
| Distributivity | |
| Identity | , |
| Domination | , |
| Complement | , , |
| De Morgan | , |

Note in particular the identity (relative to a universe ): difference can be expressed through intersection and complement. Union and intersection are commutative; difference is not.
2.4 Implementing a set without a built-in set type
The rules forbid the language’s own set type (HashSet, set(), Set, …). Use
a plain array/list and maintain the distinctness invariant yourself. Every
operation reduces to one primitive — the membership test:
- membership () — scan the list and compare elements (this is
containsElement); - insertion — append only if
containsElement(A, x)is false; - construction from a raw list — insert elements one by one, so duplicates drop out automatically.
2.5 Reference algorithms
function contains(A, x):
for y in A:
if y == x: return true
return false
function union(A, B):
R ← copy of A
for x in B:
if not contains(R, x): append x to R
return R
function intersection(A, B):
R ← empty list
for x in A:
if contains(B, x): append x to R
return R
function difference(A, B): # A \ B
R ← empty list
for x in A:
if not contains(B, x): append x to R
return R
function complement(A, U): # elements of U not in A
return difference(U, A)
2.6 Complexity
With a list representation, contains is , so union, intersection,
and difference cost . This is acceptable for the work. A
hash-based or sorted representation would reduce membership to or
respectively — which is precisely what the forbidden built-in set
types do internally, and the reason for implementing the mechanism by hand here.
2.7 The expression evaluator (Tier 3)
The final tier evaluates a string such as "A intersection B union C" given a
dictionary that maps names to sets. A workable approach:
- tokenise the string into names (
A,B,C) and operator words (union,intersection,difference,complement); - look up each name in the dictionary to obtain its set;
- fold the operators over the operands, applying them left to right.
Left-to-right evaluation reproduces the expected output of the example:
A intersection B union C gives (see
4task.md). Reuse the Tier 1–2 functions inside the evaluator — do not
re-implement the operations.