7.1 SetOperators.pdf

(323 KB) Pobierz
Set Operators
The operators are given in the right hand side table
Here we are taking the following sets and performing different set operators.
s = { 1,2,3,4,5,6,7,8,9,10 }
A = { 1,2,3,5,7 }
B = { 5,7,9,10,11 }
Union
means combining both sets and removing duplicates
C=A
|
B
C
-
|
-
=> A
|
B = { 1,2,3,5,7,9,10,11 }
Taking result of A , B in var C
pipe / union symbol
Intersection
means giving common elements
C=A
&
B
C
-
&
-
=> A
&
B = { 5,7 }
Taking result of A , B in var C
Intersection symbol
Difference
of A - B means take all elements of A except those common in B
C=A
-
B
=> A
-
B = { 1,2,3 }
Symmetric Difference
means take all elements of A and B except those common to both sets
C=A
^
B
C
-
^
-
=> A
^
B = { 1,2,3,9,10,11 }
Taking result of A , B in var C
Symmetric difference
#union
#intersection
#difference
#symmetric difference
Now let us check other operators.
< , >
are useful for checking proper subset (or) proper superset
<= ,>=
they consider equal sets as well while checking subset and superset
==
they check if two sets are equal or not
!=
they check if two sets are not equal
#11 Is in B but not in S so that’s why false
in , not in
are also know as membership operators
They check if an element is present in a set or not and return boolean type as result.
Note :
If you store the result of A | B in A, then the result will be go in A itself. So, You can write this as
A | B ( same as A = A | B )
A & B ( same as A = A & B )
A - B ( same as A = A - B)
Zgłoś jeśli naruszono regulamin