Homework for Week 13
Dictionaries
This week we are going to get an insight into dictionaries. Before the next class session, watch the videos about dictionaries, data structures that are useful for storing related sets of data:
Slides in English
Textbook in English
Quiz
Go to Moodle and take the test on video lectures.
Examples
NB! Make sure that your text files (.txt) are in the same folder as your Python code. Otherwise, Python will not find these files.
Example 1. Meetings
The file meetings.txt contains records of meetings. Each number in the file indicates a day of the week when an appointment takes place (note that this is not the number of appointments): 1 means Monday, 2 means Tuesday, etc. The following program reads the data from the file, creates a dictionary, and prints its keys and the values on the screen. The keys contain the days of a week, and the values are the numbers of planned appointments on that day.
def day_of_week(n): days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] return days[n-1] ffile = open("meetings.txt") meetings = dict() for line in ffile: try: number = int(line) day = day_of_week(number) meetings[day] = meetings.get(day,0) + 1 except: print("Invalid value") print(meetings)
Example 2. Inventory
This program helps compile an inventory of all fruits in a shop. Dictionary inventory contains the data about how many fruits there should be in the shop (fruit names and their amounts). The program has a loop that repeatedly asks the user for a fruit. If the fruit is in the dictionary, the program outputs the amount of that fruit in the shop and asks the user if he wants to update the fruit's amount. If the fruit is not in the dictionary, the program asks the user about the fruit's amount and adds a new entry into the dictionary. The program continues asking until the user enters “done”.
inventory = {'apple': 430, 'banana': 312, 'orange': 525, 'pear': 217} while True: fruit = input('Enter a fruit (done to quit): ') if fruit == 'done': break if fruit in inventory: print('Stock information for', fruit, 'is', inventory[fruit]) upd = input('Do you want to update it? (yes/no) ') if upd == 'yes': number = int(input('How many ' + fruit + ' do you have? ')) inventory[fruit] = number else: print('There is no information about', fruit, 'in the shop') number = int(input('How many ' + fruit + ' do you have? ')) inventory[fruit] = number
Exercises
1. Dictionary
A tool that is very convenient when learning a new language, is a dictionary, where we can look up words and add new words. Write a program that repeatedly prompts the user for a foreign word. If the word is in the dictionary, then the program prints its translation. If there is no such word in the dictionary yet, the program should ask for its translations and add the word together with its translation to the dictionary.
If the user is asked for a new word and the user enters done, the program stops prompting and outputs the total number of words in the dictionary and all words together with their translation (one word and translation pair per line).
Here is an example of program output:
Enter a word (done to quit): stjärtand There is no information for stjärtand What does stjärtand mean? pintail Enter a word (done to quit): silkesapa There is no information for silkesapa What does silkesapa mean? marmoset Enter a word (done to quit): brushane There is no information for brushane What does brushane mean? ruff Enter a word (done to quit): silkesapa silkesapa means marmoset Enter a word (done to quit): brushane brushane means ruff Enter a name (done to quit): done There are 3 entries in the dictionary: stjärtand - pintail silkesapa - marmoset brushane - ruff
Optional. Modify your program so that it reads the dictionary from the file at the beginning of the session and writes the new dictionary to the same file at the end of the session so that words from previous sessions are already in the dictionary when the new session starts.
2. Birthdays
A text file contains birthdays in the format yyyy-mm-dd (see example).
Write a function create_dictionary that takes a filename as a parameter. The function reads the data from the file and creates a dictionary with the following structure. The keys are years the and values are the numbers of people who have been born on that year. Both keys and values are integers.
>>> create_dictionary('dates.txt') {1999: 3, 1993: 3, 2000: 2, 1991: 2, 1988: 1, 1997: 3, 1996: 6, 1998: 2, 1974: 1, 1982: 1, 1992: 1, 1989: 3, 1977: 1, 1994: 1, 1981: 2, 1995: 1, 1986: 2, 1983: 1}
Write a program that prompts the user for a filename and uses the function create_dictionary to output the top 3 years that have the most birthdays in that file, together with the numbers of birthdays (one birthyear-number pair on each line).
Enter filename: dates.txt The top 3 years that have the most birthdays are: 1996 - 6 birthdays 1999 - 3 birthdays 1997 - 3 birthdays
Submit your solutions
Go to Moodle and upload your solutions under homework for Week 13. The programs of exercises 1 and 2 should have the names home1.py and home2.py, respectively.
(The optional part of exercise 1 should be a separate program that can have any name; there is no autograder for the optional part of exercise 1.)