11.7 Accumulating Results From a Dictionary

From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython1 class period2 exercises2 graded tests

README.md

11.7 Accumulating Results From a Dictionary

Just as we have iterated through the elements of a list to accumulate a result, we can also iterate through the keys in a dictionary, accumulating a result that may depend on the values associated with each of the keys.

For example, suppose that we wanted to compute a Scrabble score for the Study in Scarlet text. Each occurrence of the letter 'e' earns one point, but 'q' earns 10. We have a second dictionary, stored in the variable letter_values. Now, to compute the total score, we start an accumulator at 0 and go through each of the letters in the counts dictionary. For each of those letters that has a letter value (no points for spaces, punctuation, capital letters, etc.), we add to the total score.

f = open('scarlet2.txt', 'r')
txt = f.read()
# now txt is one long string containing all the characters
letter_counts = {} # start with an empty dictionary
for c in txt:
    if c not in letter_counts:
        # we have not seen this character before, so initialize a counter for it
        letter_counts[c] = 0

    #whether we've seen it before or not, increment its counter
    letter_counts[c] = letter_counts[c] + 1

letter_values = {'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f':4, 'g': 2, 'h':4, 'i':1, 'j':8, 'k':5, 'l':1, 'm':3, 'n':1, 'o':1, 'p':3, 'q':10, 'r':1, 's':1, 't':1, 'u':1, 'v':4, 'w':4, 'x':8, 'y':4, 'z':10}

tot = 0
for letter in letter_counts:
    if letter in letter_values:
        tot = tot + letter_values[letter] * letter_counts[letter]

print(tot)

Line 18 is the tricky one. We are updating the variable tot to have its old number plus the score for the current letter times the number of occurrences of that letter. Try changing some of the letter values and see how it affects the total. Try changing txt to be just a single word that you might play in Scrabble.

Check your Understanding

Exercise 1

1. The dictionary travel contains the number of countries within each continent that Jackie has traveled to. Find the total number of countries that Jackie has been to, and save this number to the variable name total. Do not hard code this!

Open the 01 folder and edit main.py. Run Tests checks it.

Exercise 2

2. schedule is a dictionary where a class name is a key and its value is how many credits it was worth. Go through and accumulate the total number of credits that have been earned so far and assign that to the variable total_credits. Do not hardcode.

Open the 02 folder and edit main.py. Run Tests checks it.

For teachers

11.7 Accumulating Results From a Dictionary

Students read Foundations of Python Programming's “Accumulating Results From a Dictionary” in CS Room and work through its 2 Python coding exercises (2 graded).

  • Time: 1 class period
  • Tests: 2 in 2 graded exercises, set up for you when you import the lesson

Exercises

FolderGradingTests
01Autograded1
02Autograded1