BYU logo Computer Science

To start this assignment, download this zip file.

Lab 5a — Dictionaries

Reminder, you should work in teams of 2 or 3 when solving the lab problems. Learning to code together is an important part of this class.

Preparation

1 minute

Download the zip file for this lab, located above. This zip file has code that you will use for this assignment. Extract the files and put them in your cs110 directory in a folder called lab5a.

Review on dictionaries

Here is a short reminder of how to use dictionaries, from the guide on dictionaries:

# a dictionary
meals = {
    'breakfast': 'yogurt, granola, and fruit',
    'lunch': 'Cupbop',
    'dinner': 'lasagna and salad'
}

# get the value for a key
food = meals['dinner']

# get the value for a key
# and use a default value if the key does not exist
food = meals.get('brunch', 'buttered toast and apple juice')

# loop through all the key, value pairs
for meal, description in meals.items():
    print(f'For {meal} we are serving {description}. 😋')

# check if a key exists
if 'dinner' in meals:
    print("Yay, we are eating dinner.")

Library

20 minutes

We have given you code in library.py that mostly implements a library. The library comes with a dictionary that maps titles to authors:

library = { 'In Search of Lost Time': 'Marcel Proust',
                'Ulysses' : 'James Joyce'
}
  1. Review the code that is written for you to see what it does.

  2. Write the function called who_is_this_book_by(library, title). This function finds the author of a book in the library, given its title. After you write this code, the program should print:

Pride and Prejudice is by Jane Austen
  1. Write the function called do_you_have_these_books(library, books). This function checks whether the library has any of the books in a list, each of which is a title. After you write this code, the program should print:
Yes, we have Pride and Prejudice by Jane Austen.
No, we don't have Invisible Man, sorry!
Yes, we have Anna Karenina by Leo Tolstoy.
No, we don't have To Kill a Mockingbird, sorry!
  1. Write the function called what_books_do_you_have_by(library, requested_author). This function prints a list of all the titles the library has that are written by this author. After you write this code, the program should print:
We have the following books by Fyodor Dostoyevsky:
* The Brothers Karamazov
* Crime and Punishment

Discuss with the TA:

  • How did you implement these functions? Show a solution and discuss.
  • Is there anything you don’t understand about dictionaries?
  • Make sure everyone understands every line of code we supplied.

Round up

15 minutes

We have given you some code in round_up.py that is supposed to do the following:

  • reads a CSV file that contains lines of “name,grade”
  • rounds up everyone’s grade, using a dictionary
  • writes a CSV file that contains lines of “name,new_grade”, using the new grades
  1. Review the code that is written for you to see what it does.

  2. To complete this code, you need to write one function. change(line, grades) takes a single line that has the form “name,grade\n”. It returns a new line of the form “name,new_grade\n”.

After you write this code, run the program with:

python round_up.py grades1.csv new_grades1.csv

Check the file new_grades1.csv to be sure you got it to work correctly.

  1. Now run the code with:
python round_up.py grades2.csv new_grades2.csv

If you get an error when you run it with this new file, that is OK! We have been sneaky and put two grades in this file that don’t appear in the dictionary. One student has an ‘A+’ and another has a ‘P’. You need to change your code so that it does not assume that the original grade is in the grades dictionary. You will need to use get(). See above.

Discuss with the TA:

  • How did you implement this function? Show a solution and discuss.
  • Is there anything you don’t understand about dictionaries?
  • Make sure everyone understands every line of code we supplied.

Grading

To finish this lab and receive a grade, take the canvas quiz.

Solution

We are providing a solution so you can check your work. Please look at this after you complete the assignment. :-)