from __future__ import division, print_function
We have a dictionary that links a country to its capital:
countryCapitals = {
'Ethiopia': 'Addis Abada',
'Canada': 'Ottawa',
'Iran': 'Teheran',
'Turkey': 'Ankara'
}
What will countryCapitals.keys()
output?
What will countryCapitals.values()
output?
What will len(countryCapitals)
output?
What will 'Iran' in countryCapitals
output?
What will 'Iraq' in countryCapitals
output?
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.
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
.
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.
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.
Compare addFriend(d, name, age)
and modifyAge(d, name,age)
functions. What does it tell you about the dictionaries property?
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
.
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.
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.
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 ''
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'}
def mergeListsAsDict(lNames, lGrades):
# Complete the function here
return {}
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
dictResult = {
'Daniel': 'F',
'Basi': 'B',
'Rupal': 'A',
'Dim': 'C',
'Leello': 'A',
'Mike': 'B'
}
assert mergeListsAsDict(lNames, lGrades) == dictResult, 'The solution is incorrect'
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 []
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']}
def getFriendsOfFriends(friends):
# Complete the function here
return {}
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)
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'
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}
def getDistinctEntriesCount(entries):
# Complete the function here
return {}
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)
distinctEntries = {
'Segno': 4,
'Maksegno': 7,
'Erob': 3,
'Hamus': 2,
'Arb': 7
}
assert distinctEntries == getDistinctEntriesCount(entries), 'The solution is incorrect'