1 / 17

COMPSCI 101 Principles of Programming

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?.

laddie
Download Presentation

COMPSCI 101 Principles of Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 101Principles of Programming Lecture 26 – Counting Dictionaries

  2. Learning outcomes • At the end of this lecture, students should be able to: • Create and use a counting dictionary COMPSCI 101 - Principles of Programming

  3. 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

  4. Histogram Example COMPSCI 101 - Principles of Programming

  5. 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

  6. 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

  7. 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

  8. Answer COMPSCI 101 - Principles of Programming

  9. Helper Functions COMPSCI 101 - Principles of Programming

  10. 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

  11. Answer COMPSCI 101 - Principles of Programming

  12. 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

  13. Answer COMPSCI 101 - Principles of Programming

  14. Helper Functions COMPSCI 101 - Principles of Programming

  15. More Helper Functions COMPSCI 101 - Principles of Programming

  16. Summary • Creating Dictionaries • Retrieving Items from Dictionaries • Using Dictionaries as Histograms COMPSCI 101 - Principles of Programming

  17. Tomorrow • Angela is back with more Dictionary Examples! COMPSCI 101 - Principles of Programming

More Related