Homework for Week 10
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 which 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 amount of the fruit. If the fruit is not in the dictionary, the programs asks the user about the amount of the fruit 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. 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 of the dictionary are names of weekdays (i.e. "Monday", "Tuesday" etc), and the value of each key is the number of people who have been born on that weekday. At the end the function returns the dictionary.
>>> create_dictionary('dates.txt') {'Saturday': 11, 'Wednesday': 4, 'Friday': 8, 'Thursday': 10, 'Sunday': 5, 'Tuesday': 4, 'Monday': 9}
To find the weekday, use Python's standard module datetime:
import datetime date = datetime.datetime(2020, 4, 9) print(date.weekday())
2. 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.
Submit your solutions
Go to Moodle and upload your solutions under homework for Week 10. The programs of exercises 1 and 2 should have the names home1.py and home2.py, respectively. The optional part of exercise 2 should be a separate program that can have any name; there is no autograder for the optional part of exercise 2.