Lecture 5, Part 2 Exercises

Before starting, please run the following cell

In [ ]:
from __future__ import division, print_function 

Question 1

We have a dictionary that links a country to its capital:

countryCapitals = {
    'Ethiopia': 'Addis Abada', 
    'Canada': 'Ottawa', 
    'Iran': 'Teheran', 
    'Turkey': 'Ankara'
}

1.1

What will countryCapitals.keys() output?

1.2

What will countryCapitals.values() output?

1.3

What will len(countryCapitals) output?

1.4

What will 'Iran' in countryCapitals output?

1.5

What will 'Iraq' in countryCapitals output?

Question 2

2.1

Write a dictionary friendsAge mapping the names of five of your friends to their ages. In other words, we want a dictionary with students names as keys and ages as values.

2.2

Write a function averageFriendsAge(d) where d is a dictionary mapping the names of friends to their ages and that returns the average age of all the friends in the dictionary d.

2.3

Now, write a function addFriend(d, name, age) where d is a dictionary mapping the names of friends to their ages, name is a string and age is an integer, that returns the updated dictionary.

Use it to add Daniel, your 80 years old instructor, to your friendsAge dictionary.

2.4

Daniel drinks a magic potion and gets 60 years old younger. Write a function modifyAge(d, name, age) to update one of your friends age, where d is a dictionary mapping the names of friends to their ages, name is a string and age is an integer, that returns the updated dictionary.

2.5

Compare addFriend(d, name, age) and modifyAge(d, name,age) functions. What does it tell you about the dictionaries property?

2.6

Write a function getFriendAge(d, name) where d is a dictionary mapping the names of friends to their ages and name is a string, that returns the age of your friend.

What happens if you run it with a name not in your dictionary? In this case return -1.

Question 3

3.1

In Lecture 2 Exercices (Part 2), we wrote a function mostCommon(s) that takes in a string s and returns the most common character in a string. If no character occurs more often than other character, return any character in the string.

For example, mostCommon('salaam') = 'a'

Also, mostCommon('abc') = 'a', 'b' or 'c'

Now, we want to rewrite this function using a dictionary.

3.2

In Lecture 5 Exercices (Part 1), we wrote a function countSpiesRec(s) that given a string of spies and commoners, will return the number of spies (s characters).

For example countSpiesRec("scccscccccc") = 2.

Another example: countSpiesRec("cccc") = 0.

Also: countSpiesRec("") = 0.

Now, we want to rewrite this function using a dictionary.

3.3

Similarly, we want to write a function encodeString(s) that takes in a string s and returns a string of the encoded letters.

For example, encodeString('aabbb') returns 'a2b3'

Also, encodeString('abbbaac') returns 'a3b3c1'

Also, encodeString('') returns ''

Question 4

4.1

We are given two lists: one list (lNames) of students names as strings and one list (lGrades) of grades from A to F as strings. We want to write a function mergeListAsDict(lNames, lGrades) that takes the two lists as inputs and returns a dictionary with the names of the sudents as key and the grade as value.

For example, mergeListsAsDict(['Daniel', 'Rupal'], ['F', 'A']) returns {'Daniel': 'F', 'Rupal': 'A'}

In [ ]:
def mergeListsAsDict(lNames, lGrades):
    # Complete the function here 
    return {}
In [ ]:
lNames = ['Daniel', 'Basi', 'Rupal', 'Dim', 'Leello', 'Mike']
lGrades = ['F', 'B', 'A', 'C', 'A', 'B']

mergeListsAsDict(lNames, lGrades) 

Run the cell bellow to verify your solution

In [ ]:
dictResult = {
    'Daniel': 'F',
    'Basi': 'B',
    'Rupal': 'A',
    'Dim': 'C', 
    'Leello': 'A', 
    'Mike': 'B'
}
assert mergeListsAsDict(lNames, lGrades) == dictResult, 'The solution is incorrect'

4.2

Write a function getStudentsWithGrade(d, grade) where d is the dictionary mapping sutdents names to grades and grade is a grade (from A to F) as a string, that returns a list of students with that given grade.

For example, getStudentsWithGrade({'Daniel': 'F', 'Rupal': 'A'}, 'A') returns ['Rupal'].

Also, getStudentsWithGrade({'Daniel': 'A', 'Rupal': 'A'}, 'A') returns ['Daniel', 'Rupal'].

Also, getStudentsWithGrade({'Daniel': 'F', 'Rupal': 'A'}, 'D') returns []

Question 5

5.1

We are given a dictionary friends mapping people to their friends. We want to write a function getFriendsOfFriends(d) where d would be that dictionary mapping people to their friends, that returns anonther dictionary mapping the same keys of people to friends of their friends. We can assume each person can't be friends with themselves.

For example,

getFriendsOfFriends({'Amy': ['Daniel', 'Basi', 'Teddy'], 
                     'Teddy': ['Rupal', 'Elvis', 'Amy']})

returns {'Amy': ['Rupal', 'Elvis'], 'Teddy': ['Daniel', 'Basi']}

In [ ]:
def getFriendsOfFriends(friends):
    # Complete the function here 
    return {}
In [ ]:
friends = {
    'Basi' : ['Leello', 'Jessica', 'Corina', 'Isabelle'], 
    'Jessica': ['Rupal', 'Daniel', 'Dim'],
    'Arash': ['Will', 'Mike'], 
    'Mike': ['Will', 'Jessica', 'Arash'], 
    'Corina': ['Basi', 'Leello'],
     'Isabelle': ['Arash', 'Rupal', 'Mike'],
    'Daniel': []  # :(
}
getFriendsOfFriends(friends)
In [ ]:
friendsOfFriends = {
    'Basi': ['Rupal', 'Daniel', 'Dim', 'Leello', 'Arash', 'Mike'],
    'Jessica': [],
    'Arash': ['Will', 'Jessica'],
    'Mike': ['Rupal', 'Daniel', 'Dim', 'Will'],
    'Corina': ['Leello', 'Jessica', 'Isabelle'],
    'Isabelle': ['Will', 'Mike', 'Jessica', 'Arash'],
    'Daniel': []
}

assert getFriendsOfFriends(friends) == friendsOfFriends, 'The solution is incorrect'

Question 6

There has been a case of food poisoning at the cafeteria Monday. We want to write a function getDistinctEntriesCount(d) where d is a dictionary mapping the days of the week to a list of entries in the cafeteria, that returns another dictionary mapping the day to the number of distinct people who ate at the cafeteria each day.

For example, getDistinctEntriesCount({'Segno': ['rupal', 'rupal']}) returns {'Segno': 1}

Also, getDistinctEntriesCount({'Segno': ['rupal', 'rupal'], 'Arb': ['isa']}) returns {'Segno': 1, 'Arb': 1}

In [74]:
def getDistinctEntriesCount(entries):
    # Complete the function here 
    return {}
In [ ]:
entries = {
    'Segno': ['arash', 'arash', 'isa', 'rupal', 'arash', 'leello'],
    'Maksegno': ['leello', 'dim', 'arash', 'arash', 'isa', 'rupal', 'nati', 'arash', 'nati', 'basi'],
    'Erob': ['leello', 'dim', 'dim', 'dim', 'arash'],
    'Hamus': ['arash', 'arash', 'rupal', 'arash'],
    'Arb': ['leello', 'leello', 'isa', 'dim', 'rupal', 'corina', 'basi', 'nati']
} 
getDistinctEntriesCount(entries)
In [79]:
distinctEntries = {
    'Segno': 4, 
    'Maksegno': 7, 
    'Erob': 3, 
    'Hamus': 2, 
    'Arb': 7
}
assert distinctEntries == getDistinctEntriesCount(entries), 'The solution is incorrect'