BYU logo Computer Science

Lab 16 — Dictionaries, Part 3 (+tuples, +sorting, +state machines)

In this lab you will gain more practice with dictionaries, including dictionaries that store dictionaries, iterating over keys and items, sorting, and the state machine pattern. Start by downloading lab16.zip. Write all your code in dictionaries_part3.py.

Sorted ice cream ratings

Write a function called sorted_ice_cream_ratings(ice_cream_ratings). This function takes one parameter, a dictionary of ice cream ratings, that maps an ice cream flavor to its average rating among tasters. For example:

{
    'mint chocolate chip': 4,
    'strawberry': 7,
    'chocolate chip': 6,
    'chocolate': 8,
    'chocolate fudge': 9,
    'cookie dough': 8.5
}

The function prints out all the flavors and their ratings in order, sorted by ice cream flavor. For example:

chocolate -> 8
chocolate chip -> 6
chocolate fudge -> 9
cookie dough -> 8.5
mint chocolate chip -> 4
strawberry -> 7

Write a function called most_popular_ice_cream(ice_cream_ratings). This function takes one parameter, a dictionary of ice cream ratings, like above. The funcition returns a tuple containing the most popular ice cream flavor and its rating. For example, given the above dictionary it would return ('chocolate fudge', 9).

Find Students

Write a function called find_students(student_list, class_name, section_name). This function takes three parameters:

  • student_list: a dictionary that maps a class name to the students in each section
  • class_name: the name of a class
  • section_name: the name of a section

The function returns a list of all the students in that section of the class. If the class is not present or the section is not present, return an empty list.

For the student_list dictionary, each key is a class name and each value is another dictionary where the key is a section name and the value is a list of students. For example:

{
    'CS 110': {'section 1': ['Emma', 'Sarah', 'Aaron', 'Raul'],
               'section 2': ['Peter', 'Lisa', 'Brianna', 'Buddy']},
    'CS 111': {'section 1': ['Luis', 'Ali', 'Emma']}
}

Then find_students(student_list, 'CS 110', 'section 2') would return ['Peter', 'Lisa', 'Brianna', 'Buddy'].

Remember, you can use a variable to give you a reference to the inner dictionary:

>>> students = student_list['CS 110']
>>> students
{'section 1': ['Emma', 'Sarah', 'Aaron', 'Raul'], 'section 2': ['Peter', 'Lisa', 'Brianna', 'Buddy']}
>>> students['section2']
['Peter', 'Lisa', 'Brianna', 'Buddy']

Find All Students

Write a function called find_all_students(student_list, class_name). This function takes two parameters:

  • student_list: a dictionary that maps a class name to the students in each section
  • class_name: the name of a class

The function returns a list of all the students in all sections of the class. If the class is not present or the section is not present, return an empty list.

Using the dictionary above, find_students(student_list, 'CS 110') would return ['Emma', 'Sarah', 'Aaron', 'Raul', 'Peter', 'Lisa', 'Brianna', 'Buddy'].

Add Student

Write a function called add_student(student_list, class_name, section_name, student_name). This function takes four parameters:

  • student_list: a dictionary that maps a class name to the students in each section
  • class_name: the name of a class
  • section_name: the name of a section
  • student_name: the name of a student

This function adds a new entry to the dictionary it is given. For example, if the student_list dictionary is as shown above, then add_student(student_list, 'CS 111', 'section 1', 'Joel') would add an entry to the dictionary so that it now looks like this:

{
    'CS 110': {'section 1': ['Emma', 'Sarah', 'Aaron', 'Raul'],
               'section 2': ['Peter', 'Lisa', 'Brianna', 'Buddy']},
    'CS 111': {'section 1': ['Luis', 'Ali', 'Emma', 'Joel']}
}

Find all classes

Write a function called find_all_classes(student_list, student_name). This function takes two parameter:

  • student_list: a dictionary that maps a class name to the students in each section
  • student_name: the name of a student

The function returns a dictionary, where each key is a class name and each value is a section name. For example, using the dictionary above, find_all_classes(student_list, 'Emma') would return:

{
    'CS 110': 'section 1',
    'CS 111': 'section 1'}
}

Find Cheapest Gas

Write a function called find_cheapest_gas(gas_prices, kind). This function takes two parameters:

  • gas_prices: a dictionary that maps gas station name to their gas prices
  • kind: the kind of gas

The function finds the cheapest gas of that kind. For example, if the gas_prices dictionary has the following:

{
    'Chevron': {'unleaded': 3.50, 'premium': 4.50},
    'Costco': {'unleaded': 3.45, 'premium': 4.25, 'diesel': 4.00},
    'Smiths': {'unleaded': 3.57, 'premium': 4.30}
}

Then find_cheapest_gas(gas_prices, 'unleaded') will return the tuple ('Costco', 3.45).

Hat Decoding

Write a function called hat_decode(message), which takes one parameter, a string. The message is encoded in the following way:

  • '^' marks the beginning of message characters
  • '.' marks the end of message characters

The function gets all the characters between every '^' and '.', ignoring the others, and returning the result. For example:

>>> hat_decode('xx^Ya.xx^y!.bb')
'Yay!'

You can solve this using the state machine pattern and a state variable called “copying” that is True when chars should be copied to the output and False when they should be ignored.

Lessons

What we want you to get from this lab:

  • You are more comfortable creating and using dictionaries whose values are dictionaries

  • You can iterate over the keys and items in a dictionary

  • You can sort and print the items in a dictionary

  • You can use and unpack tuples

  • You can use the state machine pattern in a function

  • You can figure out what went wrong when something unexpected happens

  • Hopefully you had fun!

Points

Turn in a zip file that has your code.

TaskDescriptionPoints
Sorted ice cream ratingsYour solution works1
Most popular ice creamYour solution works1
Find studentsYour solution works1
Find all studentsYour solution works1
Add studentYour solution works1
Find all classesYour solution works2
Find cheapest gasYour solution works2
Hat decodingYour solution works1

Credits

Some problems taken from CS 106A at Stanford.