A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/Set operators





Sets describe collections of things such as numbers or people. For the exam you need to be familiar with the following terminology: For the following examples we are going to use two sets. To define a set use the curly brackets {} and place all values of that set inside them. Set A: All even naturals up to and including 12 = {0,2,4,6,8,10,12} Set B: Natural multiples of 3 up to and including 12 = {0,3,6,9,12}
 * Membership
 * Union
 * Difference
 * Intersection

Membership
Membership defines what is contained in a particular set, it makes claims as to a particular things set membership. Set membership means "is an element of" and is written by &isin;. The reverse "is NOT an element of" is written by &notin;. Let's take a look at some examples: Monday &isin; WeekDays (Monday is an element of WeekDays) Monday &notin; Colours  (Monday is NOT an element of Colours) Let us take a look at the two sets we are using as examples:
 * 0 &isin; A
 * 2 &isin; A
 * 0 &isin; B
 * 6 &isin; A AND 6 &isin; B
 * 7 &notin; B
 * 89 &notin; A

Union
When we have sets we often want to perform actions on them, for example trying to find all the unique members contained in two sets. The union(&#8746;) of A and B, denoted by A &#8746; B, is the set of all things which are members of either A or B. It is similar to the boolean function OR. The union command allows us to perform this action:
 * {1, 2} &#8746; {2,3,4} ={1,2,3,4}. (notice that we don't count 2 twice)
 * {1, 2} &#8746; {red, white} ={1, 2, red, white}.
 * {1, 2, green} &#8746; {red, white, green} ={1, 2, red, white, green}.
 * {1, 2} &#8746; {1, 2} = {1, 2}.

Let us take a look at the two sets we are using as examples: A = {0,2,4,6,8,10,12} B = {0,3,6,9,12} A &#8746; B

={0,2,3,4,6,8,9,10,12}. {3,4} &#8746; B

={0,3,4,6,9,12}. A &#8746; {7,13,red}

={0,2,4,6,7,8,10,12,13,red}.

B &#8746; B

={0,3,6,9,12}.

Difference
Difference(\) is used to work out the members unique to one set, members that are not also in the other set. It is similar to the boolean function NAND. $$A \setminus B = \{ x\in A \, | \, x \notin B \}. $$. Meaning elements that are members of A but not members of B. Let us look at some examples:
 * {1,2,3} &#x2216; {2,3,4}  =   {1}
 * {2,3,4} &#x2216; {1,2,3}  =   {4}

Let us take a look at the two sets we are using as examples:

A &#x2216; B    =

{2,4,8,10} B &#x2216; A    =

{3,9} B &#x2216; {1,2,3,5,7,11}    =

{0,6,9,12} A &#x2216; {0,1,1,2,3,5,8,13}    =

{4,6,10,12}

Intersection
We use intersection(&#8745;) to find out all the elements that are members of one set, and also at the same time members of another set. It is similar to the boolean function AND. : x &isin; A &#8745; B = x &isin; A AND x &isin; B. Let us look at some examples:
 * {1, 2, 3} &#8745; {2, 3, 4} = {2, 3}
 * {1, 2, 3} &#8745; {bear,hen,squirrel} = Ø (this means an empty set)
 * {cat, dog, canary} &#8745; {wolf, canary, whale, cat} = {cat, canary}

Let us take a look at the two sets we are using as examples:

A &#8745; B =

{0,6,12}

A &#8745; {goat,4,6,9} =

{4,6}

A &#8745; {goat,3,9} =

It has no intersected values (Ø)

B &#8745; {3,5,7,9,11} =

{3,9}

Programming
Not every programming language offers the ability to perform set theory calculations out of the box. In Visual Basic it has only been available since VB.NET 2008, using the LINQ libraries that you have to import to get it working: