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: 11/13/2009 1 ECS 10 11/13 Announcements MIDTERM 2 -- Mon Nov 16 Open book, open notes. Bring sample programs from class, your programs, the book, etc etc. Bring a Scantron 2000 On myUCDavis: sample midterm. Topics Lists, indexing, concatenation of lists range and len functions for loops on lists and strings for loops on dictionaries and files for loops on dictionaries and files Files String methods (split, replace, strip, find) Dictionaries Exceptions sort method on lists Programming problem We will construct a program that reads in a file containing one country per line, and, for each country, a list of major exports. The program should reverse this output: it should output a file containing one export per line, and with each export, a list of countries. Solve this problem using a dictionary, in which the keys are the exports and the values are lists of countries. Programming problem Sample input: Albania, textiles, metals, oil Algeria, oil, natural gas Argentina edible oils oil grain motor vehicles Argentina, edible oils, oil, grain, motor vehicles Sample output: oil: Albania, Algeria, Argentina metals: Albania textiles: Albania edible oils: Argentina How to approach this problem? What are the entries in the dictionary? How do they get put in? Write this part first; most likely from reading a file. For each line of the file what goes into the For each line of the file, what goes into the dictionary? How do they get taken out? Querys from user for loop on dictionary 11/13/2009 2 List indexing example import random suits = ['Hearts', 'Clubs', 'Spades', 'Diamonds'] faces = ['J', 'Q', 'K'] i = random.randrange(4) num = random.randrange(13) if num < 10: print 'My card is the',num+1,'of',suits[i] else: print 'My card is the',faces[num-11],'of',suits[i] Example MC question The program: dogList = [Collie, Beagle,\...
View Full Document
This note was uploaded on 01/14/2010 for the course ECS 10 taught by Professor Amenta during the Spring '07 term at UC Davis.
- Spring '07