Exercises on Sets

Nelson

10/12/2020

Sets, Lists and Tuples

From the Datacamp Tutorial on sets.

“The major difference is that sets, unlike lists or tuples, cannot have multiple occurrences of the same element and store unordered values.”

Exercise

Create ASet, AList and ATuple, all of which contain “cat”, “dog” and “parrot”. Print the type of each.

Answer

AList = ["cat","dog","parrot"]
ATuple = ("cat","dog","parrot")
ASet = {"cat","dog","parrot"}

print(type(AList))
## <class 'list'>
print(type(ATuple))
## <class 'tuple'>
print(type(ASet))
## <class 'set'>

Exercise

Attempt to obtain the first value of each collection. How does python respond?

Answer

print(AList[0])
## cat
print(ATuple[0])
# print(ASet[0]) Fails with "TypeError: 'set' object is not subscriptable"
## cat

Exercise

Write for loops to iterate over and print the contents of these collections. Observations?

Answer

# AList
for i in AList:
    print(i)
## cat
## dog
## parrot
# ATuple
for i in ATuple:
    print(i)
## cat
## dog
## parrot
# ASet
for i in ASet:
    print(i)
## dog
## parrot
## cat

I observed that the order of items printed from the set was not what I used when I created the set.

Alternatives

Create BList, BTuple and BSet. They have the same items as the A variants, but the cat appears last instead of first.

BList = ["dog","parrot","cat"]
BTuple = ("dog","parrot","cat")
BSet = {"dog","parrot","cat"}

Exercise

Test the A and B variants for equality. What do you observe?

# The lists
print(AList == BList)
## False
# The tuples
print(ATuple == BTuple)
## False
# The sets
print(ASet == BSet)
## True

With lists and tuples, order matters, but this is not true for sets.

Create the C Variants

To each of the collection definitions, add another “dog” as a fourth entry.

CList = ["cat","dog","parrot","dog"]
CTuple = ("cat","dog","parrot","dog")
CSet = {"cat","dog","parrot","dog"}

Exercise

Print each of these and observe the difference.

Answer

print(CList)
## ['cat', 'dog', 'parrot', 'dog']
print(CTuple)
## ('cat', 'dog', 'parrot', 'dog')
print(CSet)
## {'dog', 'parrot', 'cat'}

Observe that entering the second “dog” changed the list and the tuple, but not the set.

Exercise

Try the len function on the C variants.

Answer

print(len(CList))
## 4
print(len(CTuple))
## 4
print(len(CSet))
## 3

Sets and Dictionaries

These two data structures share the curly. In the case of a non-empty curly a set can be distinguished from a dictionary by the absence of the : structure in the case of a set.

Example

pets_dict = {"dog":2,"cat":1,"parrot":0}

pets_set = {"Fido","Rover","Felix"}

print(pets_dict)
## {'dog': 2, 'cat': 1, 'parrot': 0}
print(pets_set)
## {'Fido', 'Rover', 'Felix'}

This implies that two curlies with nothing included must be an empty dictionary. An empty set is given by set().

Exercise

Add a python to the dictionary and the set. The pythons name is “Cool guy”.

Answer

pets_dict["Python"] = 1
pets_set.add("Cool guy")

print(pets_dict)
## {'dog': 2, 'cat': 1, 'parrot': 0, 'Python': 1}
print(pets_set)
## {'Fido', 'Rover', 'Felix', 'Cool guy'}

Note that sets are quite mutable.

Exercise

The cat died, so we want to update the set and dictionary.

Answer

pets_dict["cat"] = 0
pets_set.discard("Felix")

print(pets_dict)
## {'dog': 2, 'cat': 0, 'parrot': 0, 'Python': 1}
print(pets_set)
## {'Fido', 'Rover', 'Cool guy'}

Exercise

Note that the set doesn’t print in alphabetical order. Use the sorted function to “solve” this problem and create sorted_pets. What is the type of this new object?

Answer

sorted_pets = sorted(pets_set)

print(type(sorted_pets))
## <class 'list'>
print(sorted_pets)
## ['Cool guy', 'Fido', 'Rover']

Two Functions

The function set() turns a list into a set. A set is unduplicated. the function list() turns a set into a list.

Use these facts to write a function undup(), which converts a list with duplications into an unduplicated list. Test it on [1,1,2,3,4,4,5,1,2,6]

Answer

def undup(li):

    return(list(set(li)))
    
print(undup([1,1,2,3,4,4,5,1,2,6]))
## [1, 2, 3, 4, 5, 6]

Range to Set

Does the set() function work on range objects?

Answer

I100 = set(range(1,101))

print()
print(I100)
## {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}
print()
print(len(I100))
## 100
print()
print(type(I100))
## <class 'set'>

Python somewhat awkwardly displays the result as a “set of a list”.

Let’s use this set as our universe to discuss some operations on its subsets. How many subsets does it have?

Answer

\[ 2^{100} \]

Rename I100 as the Universe

Universe = I100

Create Subsets

Start with Evens, the subset of even numbers.

Answer

Evens = set()

for n in Universe:

    if n%2 == 0:
    
        Evens.add(n)
# Check    
print(Evens)
## {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100}

There is an alternative to this standard code snippet. We can create the subset using a comprehension. I’ll create the set Evens_comp and see if it is identical to Evens.

Evens_comp = {x for x in Universe if x%2 == 0}

print(Evens_comp == Evens)
## True

This notation “x for x in” seems redundant, but it has a purpose. We could compute the squares of the even numbers as follows.

Evens_squared = {x**2 for x in Universe if x%2 == 0}

print(Evens_squared)
## {256, 1024, 2304, 4096, 4, 900, 1156, 3844, 4356, 6400, 8836, 9604, 10000, 16, 144, 400, 784, 1296, 1936, 2704, 3600, 4624, 5776, 9216, 36, 676, 1444, 3364, 4900, 8100, 7056, 7744, 64, 576, 1600, 3136, 196, 324, 2116, 2500, 5184, 6084, 6724, 8464, 100, 484, 1764, 2916, 5476, 7396}

Exercise

Create the set of odds using comprehension.

Answer

Odds = {n for n in Universe if n % 2 != 0}
print(Odds)
## {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99}

Note that the symbol we use to build the comprehension does not appear in the result. We just have to use it consistently when we define the comprehension. Any valid name would produce the same result.

Exercise

Create the set HalfOne consisting of every number 50 or less. Hint: Put a conditional at the end of the comprehension definition.

Answer

HalfOne = {n for n in Universe if n <= 50}
print(HalfOne)
## {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}

Exercise

Create the sets Div3 , Div4, and Div5 containing the numbers divisible by 3, 4, and 5 respectively.

Answer

Div3 = {n for n in Universe if n % 3 == 0}
Div4 = {n for n in Universe if n % 4 == 0}
Div5 = {n for n in Universe if n % 5 == 0}

Exercise

Create and print the intersection of Div3 and Div5.

Answer

Div3and5 = Div3 & Div5
print(Div3and5)
## {75, 45, 15, 90, 60, 30}

Exercise

Create and print the union of Div3 and Div5.

Answer

Div3or5 = Div3 | Div5
print(Div3or5)
## {3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45, 48, 50, 51, 54, 55, 57, 60, 63, 65, 66, 69, 70, 72, 75, 78, 80, 81, 84, 85, 87, 90, 93, 95, 96, 99, 100}

Exercise

Create and print the set difference of Div3 and Div5.

Answer

Div3not5 = Div3 - Div5
print(Div3not5)
## {3, 6, 9, 12, 18, 21, 24, 27, 33, 36, 39, 42, 48, 51, 54, 57, 63, 66, 69, 72, 78, 81, 84, 87, 93, 96, 99}

Exercise

Create and print the symmetric difference of Div3 and Div5.

Answer

DivOneOf3and5 = Div3 ^ Div5
print(DivOneOf3and5)
## {3, 5, 6, 9, 10, 12, 18, 20, 21, 24, 25, 27, 33, 35, 36, 39, 40, 42, 48, 50, 51, 54, 55, 57, 63, 65, 66, 69, 70, 72, 78, 80, 81, 84, 85, 87, 93, 95, 96, 99, 100}

Complement

There is no built-in complement function or operator in python since there is no default universe. However, once you have defined a universe, you can use the difference operator with this set to define a complement function.

Create a function comp() for our example. Test it by seeing if the complement of Evens is Odds.

Answer

def comp(s):
    return Universe - s

print(comp(Evens) == Odds)    
## True

Union Distributes over Intersection

In general, the theory of boolean algebra says that for any sets, A, B, and C, the following is true.

\[ A\cup(B\cap C) = (A\cup B)\cap(A\cup C) \]

Show that this is true for an example using Div3, Div4 and Div5

Answer

leftside = Div3 | (Div4 & Div5)
rightside = (Div3 | Div4) & (Div3 | Div5)
print(leftside == rightside)
## True