This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Module 10 Programming as a productivity tool Putting it all together CS 116: Introduction to Computer Science 2 Daniel G. Brown/Troy Vasiga, University of Waterloo 10.1 Purpose of Module 10 Design choices in Python programs A case study in Python. 10.2 1 Choosing between different ways to store data Why use different structures? Why do we have lists and classes and dictionaries and tuples? Why so many different ways to store data? Each of these storage tools answers a different question . In different scenarios, one or the other may be more appropriate. Of course, we can implement some of them with the others! 10.3 1.1 When to use lists? When to use lists? Lists are a good choice when the order of the objects matters. Example 1 . For example, consider an architectural history website. All of the buildings in its collection are in order by the date that the building was constructed. If we want to be able to restrict a search to the members of the collection that was built after 1950, its handy to have a list. We can search (using binary search) for the first building built after 1950. That building, and all subsequent items in the list, are the ones were interested in. If wed used a dictionary, we couldnt easily search for a year in this way: the keys of a dictionary have to be unique, and multiple buildings will be created in some years. 10.4 More about using lists It also makes sense to use lists when the ability to directly access members of the list (as, say, in books  ) matters. Example 2 . Suppose we run a human evolution laboratory, and we want to study the evolution of the Y chromosome. We might keep a collection (a database) of Y chromosome sequences for a collection of people. Knowing what symbol each of those people has at position 12,025,774 in their DNA sequence might be really handy. Its best to be able to identify that without looking at all of its twelve million predecessors! 10.5 1 Another good thing about lists Lists have a fairly low overhead. We could have built that DNA system where every symbol in the DNA was an entry in a dictionary, keyed by its position. Looking things up would be fast. But storing it would be huge! Wed have to write down all of those keys! If data belong in a simple order, and that order matters, use lists. 10.6 1.2 When to use dictionaries? What about dictionaries? Dictionaries are immensely powerful when the primary operation is searching . Theyre easier to maintain than lists. Theyre incredibly fast to search. Example 3 . Voter registries have to keep track of where people should be voting, and should be able to allow for people moving....
View Full Document
- Fall '09
- Computer Science