Compsci 06, Spring 2011, Test 2 Practice, April 11

Name____________________   net-id _________       

Name____________________   net-id _________       

Name____________________   net-id _________       
  1. In writing code to store the names of students on the wait-list for a course the programmer uses a list instead of a set. What's a good reason to use a list for this task (rather than a set)?
    
    
  2. Which statement creates a list of the elements in common to two lists a and b. More than one can be correct.

    1. list(set(a)&set(b))

    2. [x for x in a if x in b]

    3. [x for x in set(a) if x in set(b)]

  3. Two lists are given:
    
     x = [1,2,3]
     y = [5,6,7]
    
    
  4. What is the value of [(a,y[i]) for i,a in enumerate(x)]

    1. [(1,5), (2,6), (3,7)]

    2. [(5,1), (6,2), (7,3)]

    3. [(0,5), (1,6), (2,7)]

  5. What is the value of [(a,b) for a in x for b in y]?

    1. [(1,5), (2,6), (3,7)]

    2. [(1,5),(1,6),(1,7),(2,5),(2,6),(2,7),(3,5),(3,6),(3,7)]

    3. [(1,7), (2,6), (3,5)]

  6. The list comprehension [x for x in range(100) if x % 3 == 0] represents the list [0,3,6,...,96,99], i.e., multiples of three less than 100. Write a list comprehension for multiples of seven between 500 and 600 (inclusive).
    
    
    
    
  7. Write a list comprehension for perfect squares less than 500, i.e., [0,1,4,9,16,25,...,441,484]
    
    
    
    
  8. Data for student groups at Duke is stored in a file as below:
    Duke Chronicle:svp9,tlm,fro72
    Student Government:tlm,ola,ezp9
    Ski Club: fro72, frp9, tlm,ght
    
    
    Each group/club is the first part of a line, followed by a colon, followed by the net-ids of those in the group/club. Write a function that has a filename as a parameter and which returns a dictionary in which keys are net-ids/strings and the associated value is a list of organizations to which the person with the net-id belongs to. For example in the file above:
       tlm : ["Duke Chronicle", "Student Government", "Ski Club"]
    
    Complete the function below:

      def groups(filename):
          """
          filename is name of a file in proper format
          returns dictionary with net-id as key and value
          a list of groups/organizations
          """
    
          f = open(filename):
          
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
          close(f)