During session 11
Exercises
NB! The practical session exercises are always provided enough and to spare with an eye to fast / experienced students have exercises to solve. To this end, do not get upset if you do not get on all the task in class. Take your time and complete the tasks at home!
1. A piece of code for a fantasy video game
In this task, write a piece of code for a fantasy video game. The code will help keep a record on the characters and their inventory. First, create a dictionary. The items of the inventory must be kept as keys of the dictionary (data type – string). The values in the dictionary are integers; they show how many of each item a player has. For example, a dictionary {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12} means a player has 1 rope, 6 torches, 42 gold coins, and so on.
Next, write a function displayInventory that takes a dictionary and displays it as follows:
Inventory: 1 rope 42 gold coin 6 torch 1 dagger 12 arrow Total number of items: 62
Hint: You can use a for loop to loop through all the keys in the dictionary.
2. Fantasy game inventory v.2
Imagine that vanquished dragon’s loot is represented as a list of strings like this:
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
Write a function called addToInventory(inventory, addedItems) where the inventory is a parameter for a dictionary representing one’s inventory (like in the previous exercise) and the addedItems is a parameter for a list (like dragonLoot). The addToInventory() function should return a dictionary that represents the updated inventory. Note that the values in the addedItems list might not be unique. Display the new inventory using the function from the previous exercise.
Example of the output (the first two programs together):
Inventory: 1 rope 42 gold coin 6 torch 1 dagger 12 arrow Total number of items: 62 Dragon's loot: Inventory: 45 gold coin 2 dagger 6 torch 1 rope 12 arrow 1 ruby Total number of items: 67
3. International vehicles
An international licence plate country code is 1–3 letter abbreviation in uppercase on a small white oval plate or sticker near the number plate on the rear of a vehicle. The code shows the country where a vehicle's registration plate was issued. For example, the code of Estonia is EST.
Create a .txt file (a database) for a border guard so that each line of the file contains a country code and the name of that country separated by a space.
Example of file countries.txt:
ER Eritrea FIN Finland F France H Hungary LT Lithuania EST Estonia S Sweden
Write a function dictionary_from_file which takes the name of the file as an argument and returns a dictionary based on the content of the file (the keys in the dictionary are country codes and the values are country names).
Example of the function call (using the above-mentioned file):
>>> dictionary_from_file("countries.txt") {'EST': 'Estonia', 'F': 'France', 'H': 'Hungary', 'S': 'Sweden', 'LT': 'Lithuania', 'FIN': 'Finland', 'ER': 'Eritrea'}
Next write a function codes_to_names with two arguments: one is a list with country codes and the other one is a dictionary of country code - country name pairs. The function has to return a list with the names of the respective countries. If any country code in the list is unknown, then in returning list the name of that country must be noted as Unknown.
>>> d = dictionary_from_file("countries.txt") >>> codes = ['EST', 'CO', 'FIN', 'UY', 'BR', 'LT', 'ER'] >>> codes_to_names(codes, d) ['Estonia', 'Unknown', 'Finland', 'Unknown', 'Unknown', 'Lithuania', 'Eritrea']
Finally write a program that
- prompts the user for a file name;
- prompts the user for a string which contains country codes of vehicles crossing the border, separated by spaces;
- using the functions created above outputs the names of the countries where the vehicles were registered (each on separate line).
Example of program output:
Enter a file name: countries.txt Enter codes: EST CO FIN UY BR LT ER Estonia Unknown Finland Unknown Unknown Lithuania Eritrea
4*. Children and parents
The file children.txt contains parent's personal identification code, space, and child's personal identification code on each line. Another file names.txt contains person's personal identification code, space, colon, space and name of a person on each line. Assume that there are no duplicate names in the file and there is a name in file names.txt for each personal code from file children.txt.
Write a program that outputs one row per parent: parent's name, colon, space, and then the names of parent's children separated by commas and spaces. For example, using the given files, the following lines should be printed on the screen (the order of parents or their children is not important):
Madli Peedumets: Robert Peedumets, Maria Peedumets Peeter Peedumets: Robert Peedumets, Maria Peedumets Kadri Kalkun: Liisa-Maria Jaaniste Karl Peedumets: Peeter Peedumets
Hints:
- Read the file names.txt and add the data into a dictionary (keys in the dictionary are personal identification codes and their values are names).
- Read the file children.txt and add the data into a second dictionary (keys are parents' names and the values are lists with children names). NB! Use dictionary created in the first item to get the names of parents via identification codes
- Loop through all the keys of the second dictionary, format and output the information about parents and children.