Creator: Joon Kim
Reviewer 1: Wassim Marrakchi
Reviewer 2: Abebe Amare
RUN THIS NEXT SNIPPET BEFORE STARTING THE EXERCISES
def check(actual, expected):
if expected != actual:
print(
f"Function should return the value {expected}, it is returning the value {actual}")
else:
print(f"Congratulations, the test case passed!")
my_list = [3, -1, 5, 10, 15, -6, 4, 7]
Print all elements in my_list
using a for
loop.
#Todo
Print all elements in my_list
using a while
loop.
#Todo
In python, we can create dictionaries with the following simple syntax:
football_teams = {
'Barcelona': 'Spain',
'Liverpool': 'England',
'ManchesterUnited': 'England',
'Arsenal': 'Italy',
'PSG': 'France',
'RealMadrid': 'Spain',
'ACMilan': 'Italy'
}
An empty dictionary can be represented simply as empty_dictionary = {}
.
Each dictionary item is a "key" : "value" pair separated by a colon. The next item can be written into the dictionary with a comma between. We can also view the keys and the values in the dictonary with the following native python functions:
# print(football_teams.keys())
# print(football_teams.values())
print(football_teams.items())
dict_items([('Barcelona', 'Spain'), ('Liverpool', 'England'), ('ManchesterUnited', 'England'), ('Arsenal', 'Italy'), ('PSG', 'France'), ('RealMadrid', 'Spain'), ('ACMilan', 'Italy')])
Change the value associated with 'Arsenal' key to 'England' instead of 'Italy'. Check to see that the change you make is correctly registered by printing key value pairs for football_teams
.
#Todo
Check for correctness by running the code snippet below.
check(football_teams['Arsenal'], 'England')
Create a function add_team(K, V, D)
that takes in a key name K
of type string, a value name V
of type string, and a dictionary D
that adds a new football team to the existing dictionary with the correct key value pair, then returns the new dictionary with the added football team.
def add_team(K, V, D):
return
#todo
Check that the new key value pair was added correctly by running the code snippet below.
check(add_team('Juventus', 'Italy', football_teams), {
'Barcelona': 'Spain',
'Liverpool': 'England',
'ManchesterUnited': 'England',
'Arsenal': 'England',
'PSG': 'France',
'RealMadrid': 'Spain',
'ACMilan': 'Italy',
'Juventus': 'Italy'
})
Finally, create a function called count_teams(D)
that takes in a dictionary D
with football teams and returns a new dictionary with keys that indicate the distinct countries in the input dictionary D, and the values indicate the number of football teams located in those countries.
For example, for the first version of our football_teams
dictionary, count_teams(football_teams)
should return the following dictionary:
{
'Spain': 2,
'England': 3,
'Italy': 2,
'France': 1
}
def count_teams(D):
return
# Todo
Check for correctness of your function by running the code snippet below.
check(count_teams(football_teams), {
'Spain': 2,
'England': 3,
'Italy': 2,
'France': 1
})
Now imagine that we are not given the dictionary of football teams, but we are givien two arrays. The first array contains just the keys in order and the second array contains just the values in order.
teams = ['Barcelona', 'Liverpool', 'ManchesterUnited',
'Arsenal', 'PSG', 'RealMadrid', 'ACMilan', 'InterMilan']
countries = ['Spain', 'England', 'England',
'England', 'France', 'Spain', 'Italy', 'Italy']
Create a function called zip_lists(L_1, L_2)
that takes in two lists and returns a dictionary with the first list as keys and the second list as values. zip_lists(teams, countries)
should return the following:
{
'Barcelona': 'Spain',
'Liverpool': 'England',
'ManchesterUnited': 'England',
'Arsenal': 'England',
'PSG': 'France',
'RealMadrid': 'Spain',
'ACMilan': 'Italy',
'InterMilan': 'Italy'
}
def zip_lists(L_1, L2):
return
# Todo
Check for correctness by running the code snippet below.
zipped = zip_lists(teams, countries)
check(zipped, {
'Barcelona': 'Spain',
'Liverpool': 'England',
'ManchesterUnited': 'England',
'Arsenal': 'England',
'PSG': 'France',
'RealMadrid': 'Spain',
'ACMilan': 'Italy',
'InterMilan': 'Italy'
})
Using the function you built previously, count_teams
, create a function min_teams(D)
that takes in a dictionary D
of football teams and returns the country with the least amount of football teams in the dictionary. If multiple countries have the same amount of teams, return all the country names.
def min_teams(D):
return
#Todo
Check for correctness by running the code snippet below.
check(min_teams(zipped), 'France')
Now create a function max_teams(D)
with same input that returns the country with the most amount of football teams in the dictionary. If multiple countries have the same amount of teams, return all the country names.
def max_teams(D):
return
#Todo
Check for correctness by running the code snippet below.
check(max_teams(zipped), 'England')
In this section of the exercise, your goal is to implement a hash table! Recall that a hash table stores data into an array format. It uses a hashing function that generates a position to store any element or value.
As football enthusiasts, you have collected a list of players and the number of goals they have scored so far this season in a dictionary data structure named players_and_goals
.
players_and_goals = {
'Willian': 3,
'Jovic': 10,
'Pedro': 5,
'Aubameyang': 8,
'Loftus': 4,
'Giroud': 11
}
Create a hash table named hash_table
of size 10 with empty data. To represent empty data, use python's native None
.
#Todo
Now that we have an empty hash table initialized, below is a simple hash function.
def hash_func(key, hash_t):
return key % len(hash_t)
Create a function called insert_to_hash
, which takes in the key, the value, and the hash table, then inserts item into our hash table and returns the hash table. Make sure to use the hash function you wrote above to use as the position when inserting into the table.
def insert_to_hash(key, val, hash_t):
return
#Todo
Check for correctness by running the code snippet below.
test_table = [None]*10
check_against = [None, None, None, None, None, None, None, 'Messi', None, None]
check(insert_to_hash(7, 'Messi', test_table), check_against)
Create a function called insert_dictionary_to_hash
that takes in a dictonary and a hash table to insert all items from the dictionary into the hash table, and returns the final hash table. Make sure to use insert_to_hash
function you completed above.
def insert_dictionary_to_hash(dictionary, hast_t):
return
#Todo
Check for correctness by running the code snippet below.
test_table_2 = [None]*10
check_against_2 = ['Jovic', 'Giroud', None, 'Willian',
'Loftus-Cheek', 'Pedro', None, None, 'Aubameyang', None]
check(insert_dictionary_to_hash(players_and_goals, test_table_2), check_against_2)
The European football league's commissioner wants to put together the list of the hottest football stars. However, the commissioner notices that the list she receives has duplicated entries in the list.
football_stars = {
'Lionel_Messi': 'Barcelona',
'Cristiano_Ronaldo': 'Juventus',
'Lionel_Messi': 'Barcelona',
'Neymar_Jr': 'PSG',
'Kylian_Mbappe': 'PSG',
'Gareth_Bale': 'RealMadrid',
'Kylian_Mbappe': 'PSG',
'Paul_Pogba': 'ManchesterUnited',
'Lionel_Messi': 'Barcelona',
'Cristiano_Ronaldo': 'Juventus',
'Gareth_Bale': 'RealMadrid'
}
First, create a function remove_duplicates(D)
that takes in a dictionary with football players and returns a new dictionary with duplicates removed.
def remove_duplicates(D):
return
#Todo
Check for correctness by running the code snippet below.
check(remove_duplicates(football_stars), {
'Lionel_Messi': 'Barcelona',
'Cristiano_Ronaldo': 'Juventus',
'Neymar_Jr': 'PSG',
'Kylian_Mbappe': 'PSG',
'Gareth_Bale': 'RealMadrid',
'Paul_Pogba': 'ManchesterUnited'
})
The commissioner is content with the new list without duplicated information! However, another problem arose. The commissioner receives two dictionaries; one dictionary country_to_club
contains countries as keys and lists of clubs under the specific countries as values while the second dictionary club_to_player
contains star players from different clubs.
country_to_club = {
'Spain': ['Barcelona', 'RealMadrid'],
'England': ['Arsenal', 'ManchesterUnited', 'Liverpool'],
'Italy': ['InterMilan', 'ACMilan', 'Juventus'],
'France': ['PSG']
}
club_to_player = {
'Barcelona': ['Lionel_Messi', 'Gerard_Pique'],
'RealMadrid': ['Gareth_Bale', 'Sergio_Ramos'],
'Arsenal': ['Mesut_Ozil', 'Eddie_Nketiah'],
'ManchesterUnited': ['Paul_Pogba', 'Romelu_Lukaku', 'Mason_Greenwood'],
'Liverpool': ['Mohamed_Salah'],
'InterMilan': [],
'ACMilan': ['Andre_Silva'],
'Juventus': ['Cristiano_Ronaldo'],
'PSG': ['Neymar_Jr', 'Kylian_Mbappe']
}
The commissioner wants a consolidated dictionary with keys being countries and values being lists of players that play in clubs under specific countries. Create a function consolidate(D_1, D_2)
that takes in two dictionaries and returns a single dictionary with keys being distinct countries and values being lists of players that play football in that country.
def consolidate(D_1, D_2):
return
#Todo
Check for correctness by running the code snippet below.
check(consolidate(country_to_club, club_to_player), {
'Spain': ['Lionel_Messi', 'Gerard_Pique', 'Gareth_Bale', 'Sergio_Ramos'],
'England': ['Mesut_Ozil', 'Eddie_Nketiah', 'Paul_Pogba', 'Romelu_Lukaku', 'Mason_Greenwood', 'Mohamed_Salah'],
'Italy': ['Andre_Silva', 'Cristiano_Ronaldo'],
'France': ['Neymar_Jr', 'Kylian_Mbappe']
})
possible_words
function. Helper functions are allowed.¶Here is an example:
Input
words_ex = ["go", "bat", "me", "eat", "goal", "boy", "run"]
characters_ex = ['e', 'o', 'b', 'a', 'm', 'g', 'l']
Output
["go", "me", "goal"]
def possible_words(words_arr, chars_arr):
return
# Todo
Check for correctness by running the code snippet below.
check(possible_words(words_ex, characters_ex), ["go", "me", "goal"])