Solutions for the mandatory assignments on sets and dicts

1. Model an organisation of employees, management and board of directors in 3 sets.

[22]:
bd = {'Benny', 'Hans', 'Tine', 'Mille', 'Torben', 'Troels', 'Søren'}
ma = {'Tine', 'Trunte', 'Rane'}
em = {'Niels', 'Anna', 'Tine', 'Ole', 'Trunte', 'Bent', 'Rane', 'Allan', 'Stine', 'Claus', 'James', 'Lars'}

1. who in the board of directors is not an employee?

[23]:
# difference
bd - em
[23]:
{'Benny', 'Hans', 'Mille', 'Søren', 'Torben', 'Troels'}

2. who in the board of directors is also an employee?

[24]:
# Intersection
bd & em
[24]:
{'Tine'}

3. how many of the management is also member of the board?

[25]:
# intersection
len(ma & bd)
[25]:
1

4. (is) All members of the managent also an employee

[26]:
# subset
ma <= em
[26]:
True

5. (is) All members of the management also in the board?

[27]:
# subset
ma <= bd
[27]:
False

6. Who is an employee, member of the management, and a member of the board?

[28]:
#  Intersection
em & ma & bd
[28]:
{'Tine'}

7. Who of the employee is neither a memeber or the board or management?

[29]:
em - bd - ma
[29]:
{'Allan', 'Anna', 'Bent', 'Claus', 'James', 'Lars', 'Niels', 'Ole', 'Stine'}

read about sets here: https://realpython.com/python-sets/


2. Create a list of tuples from the folowing datastructure

[32]:
alphabet = {'a': 'Alpha', 'b' : 'Beta', 'g': 'Gamma'}

done through a loop:

[40]:
t = []
for key, value in alphabet.items():
    t.append((key, value))
t
[40]:
[('a', 'Alpha'), ('b', 'Beta'), ('g', 'Gamma')]

or a list comprehension

[37]:
[(key,value) for key, value in alphabet.items()]
[37]:
[('a', 'Alpha'), ('b', 'Beta'), ('g', 'Gamma')]

3. From these 2 sets:

[9]:
s1 = {'a', 'e', 'i', 'o', 'u', 'y'}
s2 = {'a', 'e', 'i', 'o', 'u', 'y', 'æ' ,'ø', 'å'}

Of the 2 sets create a:

Union

[10]:
s1 | s2
[10]:
{'a', 'e', 'i', 'o', 'u', 'y', 'å', 'æ', 'ø'}
[11]:
s1.union(s2)
[11]:
{'a', 'e', 'i', 'o', 'u', 'y', 'å', 'æ', 'ø'}

Symmetric Difference

[12]:
s1 ^ s2
[12]:
{'å', 'æ', 'ø'}
[13]:
s1.symmetric_difference(s2)
[13]:
{'å', 'æ', 'ø'}

Difference

[14]:
s1 - s2
[14]:
set()
[15]:
s1.difference(s2)
[15]:
set()
[16]:
s2 - s1
[16]:
{'å', 'æ', 'ø'}
[17]:
s2.difference(s1)
[17]:
{'å', 'æ', 'ø'}

Disjoint

[20]:
s1 & s2
[20]:
{'a', 'e', 'i', 'o', 'u', 'y'}
[19]:
s1.isdisjoint(s2)
[19]:
False

4. Date Decoder.

A date of the form 8-MAR-85 includes the name of the month, which must be translated to a number.

Create a dict suitable for decoding month names to numbers.

Create a function which uses string operations to split the date into 3 items using the “-” character.

Translate the month, correct the year to include all of the digits.

The function will accept a date in the “dd-MMM-yy” format and respond with a tuple of ( y , m , d ).

[52]:
month_dict = {'JAN' : 1, 'FEB':2, 'MAR':3,'APR':4, 'MAY':5,'JUN':6, 'JUL':7,'AUG':8, 'SEP':9,'OCT':10,'NOV':11,'DEC':12}

def manipulate_date(x):
    x = x.split('-')
    return (x[0], month_dict[x[1]], x[2])
[53]:
manipulate_date('12-APR-90')
[53]:
('12', 4, '90')

5. Party exercise

[21]:
# Define the sets of invited friends and friends who have RSVP'd
invited_friends = {"Alice", "Bob", "Charlie", "David"}
rsvp_friends = {"Charlie", "David", "Eve", "Frank"}

# Find the friends who have not RSVP'd
not_rsvpd = invited_friends - rsvp_friends

# Find the friends who were not invited but RSVP'd
not_invited = rsvp_friends - invited_friends

# Find the friends who are common in both lists (i.e., invited and RSVP'd)
common_friends = invited_friends & rsvp_friends

# Print the results
print("Friends who have not RSVP'd: ", not_rsvpd)
print("Friends who were not invited but RSVP'd: ", not_invited)
print("Friends who are common in both lists: ", common_friends)

Friends who have not RSVP'd:  {'Alice', 'Bob'}
Friends who were not invited but RSVP'd:  {'Frank', 'Eve'}
Friends who are common in both lists:  {'David', 'Charlie'}

6.

[22]:
# Define the dictionary of students and their grades
students_grades = {
    "Alice": 90,
    "Bob": 80,
    "Charlie": 85,
    "David": 70,
    "Eve": 88
}

# Update the grades of specific students
students_grades["Bob"] = 86
students_grades["David"] = 89

# Find and print the names of students who have a grade higher than 85
for student, grade in students_grades.items():
    if grade > 85:
        print(f"{student} has a grade higher than 85.")

Alice has a grade higher than 85.
Bob has a grade higher than 85.
David has a grade higher than 85.
Eve has a grade higher than 85.