Class 25 — Wednesday March 23
More dictionaries
Look both ways
Agenda
- Continue working with
dict
To do
- Examine artifacts
- Examine
dict
module documentation
- Examine
collections
epistle
Examples
- Program grocery_items.py
- Program yearly.py
- Program letter_counts.py
- In-Class Assignment word_counts.py
Problem grocery_items.py
- Given a dictionary of grocery items and their costs, the program prints out the item options (the dictionary's list of keys).
- Next, the program prompts the user to enter which items they would like to purchase, separated by commas.
- Finally, the program calculates and prints out their total cost.
Some program runs
dict_keys(['apple', 'broccoli', 'cheese', 'dijon', 'febreeze', 'eggplant'])
Enter which items you'd like to purchase, separated by spaces: apple broccoli cheese
Your total cost is $ 3.52
dict_keys(['apple', 'broccoli', 'cheese', 'dijon', 'febreeze', 'eggplant'])
Enter which items you'd like to purchase, separated by spaces: dijon eggplant febreeze
Your total cost is $ 7.67
dict_keys(['apple', 'broccoli', 'cheese', 'dijon', 'febreeze', 'eggplant'])
Enter which items you'd like to purchase, separated by spaces: dijon
Your total cost is $ 1.88
Problem yearly.py
- For every TA on the list, prompt the user to enter their year in school (for example, Nicki is a second year, so the user would enter
2
), in order to build a dictionary associating names and years.
- Next, the program prints out the dictionary of TA's and school years.
- Finally, the program calculates and prints out the TAs' average number of years at UVa.
Some program runs
Enter each TA's year in school.
What year is Nicki ?
2
What year is Rachel M ?
4
What year is Jimmy ?
3
What year is Amshala ?
2
What year is Kevin ?
3
What year is Meghan ?
4
What year is Grace ?
2
What year is Bailey ?
2
What year is Rachael C ?
3
What year is Miriam ?
3
What year is Mason ?
3
names2years = {'Nicki': 2, 'Rachel M': 4, 'Jimmy': 3, 'Amshala': 2, 'Kevin': 3, 'Meghan': 4, 'Grace': 2, 'Bailey': 2, 'Rachael C': 3, 'Miriam': 3, 'Mason': 3}
The average number of years of experience is 2.8181818181818183
Problem letter_counts.py
- First, the program prompts the user to supply a word, which is then stripped and converted to lower case.
- Then the program creates an empty dictionary, which will be populated (accumulated) with unique letters in the word, and their associated number of occurences in that word (ie, the letter count).
- For every character of the word, the program performs one of two tasks:
- If the character is not yet in the dictionary, the program adds the character to a dictionary with the appropriate letter count.
- If the character is already in the dictionary, the program retrieves that dictionary entry, and increments the associated count accordingly.
- Finally, the program prints out the newly populated dictionary.
Some program runs
Enter word: Elizabeth
letters2counts = {'e': 2, 'l': 1, 'i': 1, 'z': 1, 'a': 1, 'b': 1, 't': 1, 'h': 1}
Enter word: banana
letters2counts = {'b': 1, 'a': 3, 'n': 2}
In-class Assignment: word_counts.py
Please submit the following code before the end of class today.
- This program is very similar to letter_counts, except now we want to count the number of times each word occurs in a user-provided sentence.
- First, the program prompts the user to supply a sentence that has no punctuation, which is then stripped and converted to lower case.
- Then the program populates a dictionary with each word of the sentence and its associated number of occurences (ie, the word's count).
- Finally, the program prints out the newly populated dictionary.
A program run
Enter sentence: Whatever will be will be
words2counts = {'whatever': 1, 'will': 2, 'be': 2}
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |