BYU logo Computer Science

Lab 15 — Dictionaries, Part 2

In this lab you will gain some practice with dictionaries whose values are lists or other dictionaries. Start by downloading lab15.zip. Write all your code in dictionaries_part2.py.

Starts With

Write a function called starts_with(string_list). It takes one parameter:

  • string_list: a list of strings

The function creates and return a dictionary whose keys are the unique first characters of the strings in string_list and whose values are the count of the words that start with that character.

Starts With 2

Write a function called starts_with2(string_list). This function works the same as above except it returns a dictionary whose keys are the unique first characters of the strings and whose values are lists of words beginning with those characters, in the same order that they appear in string_list.

Shared Consonants

First, write a function called remove_vowels(word). This function takes one parameter:

  • word: a word

The function returns a new string that has all of the vowels removed. For example, 'great' becomes 'grt'.

Now, write a function called shared_consonants(filename). This function takes one parameter:

  • filename: a file with one word per line

The function removes the vowels from the words and gets just the consonants. Use your helper function. It then returns a dictionary that maps consonants to a list of words that share those same consonants. For example, if we had the following text in words.txt:

great
grate
greet
teeny
tiny
bump

then your dictionary should have:

bmp -> bump
grt -> great, grate, greet
tny -> teeny, tiny

For this assignment, the vowels are 'aeiou'.

Shared Suffix

Write a fuction called shared_suffix(words). It takes one parameter:

  • words: a list of words

It returns a dictionary whose keys are the suffixes of the strings and whose values are the words with those suffixes. A suffix is defined as the last 2 characters of a string, and a string that is less than 2 characters long has no suffix.

Remember that you can use slices to count backwards: -1 is the last character and -2 is the second to last character in the string.

A string with less than 2 characters has no suffix.

Find Price

Write a function called find_price(gas_prices, name, kind). This function takes three parameters:

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

The function returns the price of gas at a given gas station. If the gas station is not present or the kind of gas is not available, return -1.

For the gas_prices dictionary, each key is a different gas station and each value is another dictionary where the key is the kind of gas and the value is the gas price. For example:

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

Then find_price(gas_prices, 'Costco', 'premium') would return 4.25.

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

>>> prices = gas_prices['Chevron']
>>> prices
{'unleaded': 3.50, 'premium': 4.50}
>>> prices['unleaded']
3.50

Add Price

Write a function called add_price(gas_prices, name, kind, price). This function takes four parameters:

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

This function adds a new entry to the dictionary it is given. For example, if the gas_prices dictionary is as shown above, then add_price(gas_prices, 'Costco', 'diesel', 4.00) would add an entry to the dictionary so that it now looks like this:

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

As you add to the dictionary, if there is already a price for a given gas station / gas kind pair, overwrite it.

Build the Price Dictionary

Write a function called build_price_dict(filename). This function takes one parameter:

  • filename: a file with gas prices

The function returns a dictionary, where each key is a different gas station and each value is another dictionary where the key is the kind of gas and the value is the gas price. For example:

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

The format of the file provided to this function is:

name,kind,price

We have provided gas_prices.txt, which contains:

Chevron,unleaded,3.50 Smiths,unleaded,3.57 Smiths,premium,4.30
Chevron,premium,4.50 Costco,unleaded,3.45 Costco,premium,4.25 Costco,diesel,4.00

Be sure to use your add_price() function!

Lessons

What we want you to get from this lab:

  • You can create and use dictionaries that contain arbitrary types (lists, and nested dictionaries)

  • 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
Starts WithYour solution works1
Starts With 2Your solution works1
Remove vowelsYour solution works1
Shared ConsonantsYour solution works2
Shared SuffixYour solution works2
Find PriceYour solution works1
Add PriceYour solution works1
Build Price DictionaryYour solution works1

Credits

Some problems taken from CS 106A at Stanford.