$30
The starting code defines global variables bound to tuples and a dictionary as follows:
names = ("Masha", "Kevin", "Ruigang", "Vlad", "Ramesh", \
"Aditi", "Caroline", "Panos", "Chuck", "Grani", \ "Rutha", "Stan", "Qiong", "Alexi", "Carlos") cities = ("Toronto", "Ottawa", "Hamilton") testDict = {"Richard":"Toronto", "Jia-Tao":"Toronto", "Justin":"Ottawa", "Lars":"Ottawa"}
You need to define and implement the following functions:
getRandomItem(parameter: a list or tuple) - returns an item from the list or tuple
This function will randomly select an item from the list or tuple passed as an argument.
For example, if you call getRandomItem(names), then it would return one of the items from the tuple names.
createNameDictionary(parameters: none) - returns a dictionary
This function will create a new dictionary.
This function uses the global variables: names and cities (shown above)
This function returns a dictionary where each name in the names tuple is a key in the dictionary.
The key's value is randomly selected from he cities tuple using the getRandomItem() function.
Essentially the function creates a dictionary of names, where each name is randomly assigned to a city.
Your function will return this dictionary.
fromCity(parameters: dictionary, string) - returns a list
This function takes a dictionary and a string with a city name.
The function will return a list of all the names in the dictionary who are from that specified city.
For example: fromCity(testDict, "Toronto") would return a list ['Richard', 'Jia-Tao'] removePeopleFrom(parameters: dictionary, string) - no return, but modifies dictionary
This function takes a dictionary and a string. The string is a city name.
This function will delete all items in the dictionary whose value is equal to the string.
For example: removePeopleFrom(testDict, "Toronto) would modify the testDict to be {"Justin":"Ottawa", "Lars":"Ottawa"}
printNameDict(parameters: dictionary, tuple of strings) - nothing
This function takes a dictionary and a tuple of strings. The tuple items are the city names.
This function will loop through the tuple of cities and print out the name of the people who live in that city.
The peoples names will be numbered. For example: printNameDict(testDict, ("Toronto", "Ottawa")) prints: People from Toronto:
1. Richard
2. Jia-Tao
People from Ottawa:
1. Justin
2. Lars
continue on next page
If the dictionary does not have a person from a city, it should print *None*
For example, printNameDict(testDict, ("Toronto", "Ottawa", "Hamilton")) prints: People from Toronto:
1. Richard
2. Jia-Tao
People from Ottawa:
1. Justin
2. Lars
People from Hamilton *None*
main(parameters: none) - no return
Your main function will be used to test the functionality above.
Main will have two parts. Part 1 will be used to test the functions one by one (except createNameDictionary), using the testDict variable.
Part 2 will apply the function to a larger dictionary created by your function createNameDictionary(). Ideally, if you can get part 1 to work properly, part 2 will work as long as your createNamedDictionary() function is correct.
Main part 1 works as follows:
(1) Test the getRandomItem() function with argument global variable: cities
(2) Test the fromCity() function with arguments testDict and "Toronto", then testDict and "Ottawa"
(3) Test printNameDict() function with arguments testDict and tuple ("Toronto", "Ottawa")
(4) Test removePeopleFrom() function with arguments testDict and "Ottawa" To verify, call printNameDict() again
Main part 2 works as follows:
(1) create a new dictionary using the createNameDictionary() function
(2) call printNameDict with this new dictionary and the cities tuple (3) For each city (Toronto, Ottawa, and Hamilton) call the fromCity() function and print the returned lists
(4) Use the removePeopleFrom() function to remove all the people from Hamilton and Toronto from the dictionary (5) call printNameDict to show the people have been removed
See next page for an example output of Lab 5.
EXAMPLE OUTPUT OF LAB 5
Part 1 - Testing functions with `testDict'
Testing getRandomItem() function ('Toronto', 'Ottawa', 'Hamilton') item = Toronto Testing fromCity() function
['Richard', 'Jia-Tao']
['Justin', 'Lars']
Testing printNameDict() function
People from Toronto:
1. Richard
2. Jia-Tao People from Ottawa:
1. Justin
2. Lars
Testing removePeopleFrom() function
People from Toronto:
1. Richard
2. Jia-Tao People from Ottawa: *None*
Part 1
(1) Test the getRandomItem() function with argument global variable: cities
(2) Test the fromCity() function with arguments testDict and "Toronto", then testDict and "Ottawa"
(3) Test printNameDict() function with arguments testDict and tuple ("Toronto", "Ottawa")
(4) Test removePeopleFrom() function with arguments testDict and "Ottawa"
- To verify, call printNameDict() again
1. Panos 2. Grani
3. Alexi People from Ottawa:
1. Masha
2. Kevin 3. Vlad
4. Aditi
5. Caroline
6. Stan People from Hamilton:
1. Ruigang
2. Ramesh
3. Chuck 4. Rutha
5. Qiong
6. Carlos Toronto List:
['Panos', 'Grani', 'Alexi'] Hamilton List:
Ottawa List:
*None* People from Ottawa:
1. Masha
2. Kevin
6. Stan People from Hamilton: *None*