Unformatted text preview:

Test 2 Practice: Compsci 06November 15, 16, 2010Name:NetID/Login:PROBLEM 1 : (Choices)Use words, not code for the first three problems.1. Given two lists, how would you find the elements in common to both lists?2. In writing code to store the names of students on the wait-list for a course the programmer uses a listinstead of a set. What’s a good reason to use a list for this task (rather than a set)?3. You want to find which of the people in your email contacts list you sent the most email to last year.You have a log file of every email you sent. How would you use a dictionary in a Python program tohelp solve this problem?1PROBLEM 2 : (Code Review)colors = ["red", "green", "blue", "aqua", "brown", "orange","magenta", "violet", "white", "yellow"]1. What does this code print?for c in [x for x in colors if len(x) < 5]:print c2. What is printed by the code below:def rainbow(c):return c in ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]for c in [x for x in colors if not rainbow(x)]:print c3. What are the values stored in lists combo and bunch (these are tricky).x = [1,2,3,4]y = [5,6,7,8]combo = [(a,y[i]) for i,a in enumerate(x)]bunch = [(a,b) for a in x for b in y]2PROBLEM 3 : (Simple Simon Met a PI-man)Part AThe Python list comprehension below is a list of all integers between 0 and 100, e.g., [0,1,2,...100].[x for x in range(0,101)]Write list comprehensions to represent each of the following:1. all numbers between 500 and 600, inclusive2. all multiples of three less than 100, e.g., [0,3,6,...,99]3. all perfect squares less than 500, e.g., [0,1,4,9,16,25,...,484]Part BIn the Jotto program there was a function named commonCount that returns the number of letters in commonto two strings. Write a list comprehension that represents all the strings in a list words that have 3 lettersin common with the string "snipe"def commonCount(a,b):"""you can call this function, it returns an intparameters are both strings"""3PROBLEM 4 : (What’s the Point?)Write the function maxDistance that returns the maximal distance between two points in a list of pointswhere points are represented as tuples.For example the code below will print 19.209 which is the distance between (-6,6) and (9,-6) which arethe two points that are farthest apart.points = [(-7, 2), (-6, 6), (1, 2), (4, 5), (9, -6)]print maxDistance(points)Write your code below, you can call distancedef distance(a,b):"""return distance between points represented as tuplese.g., between (a_0,a_1) and (b_0,b_1)"""return math.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2)def maxDistance(points):"""return maximal distance between 2-tuples in points, a list of (x,y) points"""4PROBLEM 5 : (Banco Popular)Data for all students and their extra curricular clubs/organizations is stored in a file in the format below.Each club is named as the first item on a line of the file, and the net-ids of members of the club are givenafter the club’s name.chronicle,svp9,tlm,fro72dsg,tlm,ola,ezp9ski,fro72,frp9,tlm,ghtComplete the method below which reads this file and stores information in a global dictionary clubs inwhich the keys are net-ids and the value associated with each net-id is a list of the clubs the students withthe given net-id belongs to. For example: in the file above the student with net-id tlm is a member of allthree clubs, so that student’s entry can be thought of as:tlm = [chronicle,dsg,ski]Complete the code b e lowclubs = {} # global dictionarydef readFile(filename):file = open(filename)for line in file:file.close()5Part BWrite a list comprehension using global variable/dictionary clubs that represents a list of all netids forstudents who are members of more than 3 clubs.Part CGiven only the dictionary write the function mostMembers that returns the name of the club that has morestudent members than any other club. Reading the file with the information would make this straightforward,but for this problem you have only the dictionary in which keys are netids and values are lists of c lubs forthe student with the given netid.def mostMembers(clubs):"""returns name of club that has the most members,using information in dictionary


View Full Document

Duke CPS 006 - Compsci 06

Download Compsci 06
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Compsci 06 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Compsci 06 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?