170 likes | 318 Views
COMPSCI 101 Principles of Programming. Lecture 26 – Counting Dictionaries. Learning outcomes. At the end of this lecture, students should be able to: Create and use a counting dictionary. What is a histogram?.
E N D
COMPSCI 101Principles of Programming Lecture 26 – Counting Dictionaries
Learning outcomes • At the end of this lecture, students should be able to: • Create and use a counting dictionary COMPSCI 101 - Principles of Programming
What is a histogram? • In statistics, a histogram is a graphical representation of the distribution of data. It is an estimate of the probability distribution of a continuous variable. • A histogram is a representation of tabulated frequencies, shown as adjacent rectangles, erected over discrete intervals (bins), with an area proportional to the frequency of the observations in the interval. • The height of a rectangle is also equal to the frequency density of the interval, i.e., the frequency divided by the width of the interval. • Wikipedia COMPSCI 101 - Principles of Programming
Histogram Example COMPSCI 101 - Principles of Programming
Histograms in Python • They are just dictionaries where • the “key” is the thing you are keeping track of • The “value” is the count of that item COMPSCI 101 - Principles of Programming
Exercise Write a function named pats_candy_store_inventory() that stores the number of sweets Pat’s candy store has. COMPSCI 101 - Principles of Programming
Candy Store >>> Choose "a" (add) or "d" (delete) or "q" (quit): a Enter name of Candy: lollipop Enter amount of Candy: 5 candy lollipop amount 5 Choose "a" (add) or "d" (delete) or "q" (quit): d Enter name of Candy: lollipop Enter amount of Candy: 2 candy lollipop amount 2 Choose "a" (add) or "d" (delete) or "q" (quit): d Enter name of Candy: lollipop Enter amount of Candy: 4 candy lollipop amount 4 Cannot delete candy, you have less than this Choose "a" (add) or "d" (delete) or "q" (quit): d Enter name of Candy: gum Enter amount of Candy: 2 candy gum amount 2 Cannot delete candy, You have none. Choose "a" (add) or "d" (delete) or "q" (quit): q >>> COMPSCI 101 - Principles of Programming
Answer COMPSCI 101 - Principles of Programming
Helper Functions COMPSCI 101 - Principles of Programming
Exercise • Write a function named three_letter_anagram()that accepts a word and returns all anagrams of it. • Originally From Lecture 23! >>> three_letter_anagram("cat") ['tac', 'tca', 'atc', 'act', 'cta', 'cat'] >>> three_letter_anagram("men") ['nem', 'nme', 'enm', 'emn', 'mne', 'men'] >>> three_letter_anagram("see") ['ees', 'ese', 'ees', 'ese', 'see', 'see'] COMPSCI 101 - Principles of Programming
Answer COMPSCI 101 - Principles of Programming
Exercise • Write a function named anagram_complete() that does the anagram function from lab9.7 (advanced exercises). But now we can use a dictionary, so it will work for any size word! COMPSCI 101 - Principles of Programming
Answer COMPSCI 101 - Principles of Programming
Helper Functions COMPSCI 101 - Principles of Programming
More Helper Functions COMPSCI 101 - Principles of Programming
Summary • Creating Dictionaries • Retrieving Items from Dictionaries • Using Dictionaries as Histograms COMPSCI 101 - Principles of Programming
Tomorrow • Angela is back with more Dictionary Examples! COMPSCI 101 - Principles of Programming