BYU logo Computer Science

Lab 14 — Dictionaries, Part 1

In this lab you will gain some practice with dictionaries. Start by downloading lab14.zip. Write all your code in dictionaries_part1.py.

Letter Count

Write a function called letter_count(word). It takes one parameter:

  • word: a string

It returns a dictionary that maps each letter in the word to the number of times that letter appears.

Digit Count

Write a function called digit_count(numbers). It takes one parameter:

  • numbers: a list of non-negative integers

It returns a dictionary where each key is a digit and each value is the count of one or more numbers in the list ending with that digit.

The last digit of each num can be found by computing num % 10. For example 57 % 10 is 7, and 7 is the last digit of 57.

The dictionary should have no zero values in it, so only calculate sums for those ending digits that are present in the list.

Suffixes

Write a fuction called suffix_count(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 number of 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.

Census Names

Write a function called census_names(filename). It takes one parameter:

  • filename: a filename with census data

It returns a dictionary where each key is a letter and each value is the count of one or more last names in the census file that start with that letter. The census file has the format:

last_name,first_name,relationship,gender,race,age,marital_status

The dictionary should have no zero values in it, so only calculate sums for those letters with 1 or more names starting with that letter.

Vowel Count

Write a function called number_of_vowels(word). It takes one parameter:

  • word: a word

It returns an integer that is the number of vowels in the word. County only ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ as vowels.

Then write a function called vowel_count(filename). It takes one parameter:

  • filename: a file with one word per line

The function creates a dictionary where the keys are numbers and the values are the number of words with that many vowels. For example, if the file contains:

apple
banana
pear

Then there are two words with two vowels and one word with three vowels. The dictionary would be:

{2: 2, 3: 1}

Decrypt

Write a function called decrypt(sentence, key). This function takes two parameters:

  • sentence: a string
  • key: a dictionary that maps a character to another character

The function returns a new string, where each character is decoded using the key. For example, if the key contains:

{'p': 'b', 'v': 'a', 'r': 'n'}

Then:

>>> decrypt('pvrvrv', key)
'banana'

All whitespace and punctuation is not decrypted.

Lessons

What we want you to get from this lab:

  • You can create and use dictionaries

  • You can compute keys for dictionaries

  • You can initialize new entries for dictionaries when the key doesn’t exist

  • 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
Letter CountYour solution works1
Digit CountYour solution works1
SuffixesYour solution works2
Census NamesYour solution works2
Vowel CountYour solution works2
DecryptYour solution works2

Credits

Some problems taken from CS 106A at Stanford.