Compsci 06, Spring 2012, Test/Rec, April 5

Name____________________   net-id _________       

Name____________________   net-id _________       

Name____________________   net-id _________       
    These questions are about the code in SimpleGrammar.py, some code reproduced below.

  1. Which is the best explanation of the body of code in the statement if w.startswith("<") in the function expand below: def expand(sentence,rules): sent = "" for w in sentence.split(): if w.startswith("<"): chosen = random.choice(rules[w]) sent += expand(chosen,rules) +" " else: sent += w + " " return sent.strip()
    1. the rule chosen as a replacement for w may require expansion because it has tags in it, so the rule is passed to expand in case it's more than a simple word.

    2. because the word w starts with a < symbol we know a choice should be made to replace it, but the line assigning to sent could be replaced with: sent += chosen + " "

    3. the parameter rules is a dictionary, accessing the dictionary generates a random replacement for the key w and that replacement also starts with a < symbol.

  2. Which is the best explanation of why sent.strip() is returned rather than simply sent?

    1. all strings must be stripped in Python to ensure they can be printed.

    2. The string has an extra space at the end because a space is always the last thing concatenated to sent in the for-loop.

    3. All white-space should be removed from sent, not just leading and trailing white-space.

  3. If we want a combined-color like yellow-green or blue-red to be a possible color which string shuld be added to the list colors in the function create_content?

    1. "<color> - <color>"

    2. "yellow-green"

    3. "yellow - <color>"

    These questions refer to Sierpinsk1.py which generates a Sierpinski gasket.

  4. What is the type of parameters p1, p2 and p3 to the function sierpinski:

    1. string

    2. list

    3. tuple

  5. What ensures that the recursive function sierpinski will eventually terminate, i.e., stop generating recursive calls?

    1. lines cannot be drawn that are smaller than the screen's resolution, eventually no lines will be drawn

    2. The value of parmeter level decreases in each recursive call, eventually reaching 0 when no calls are made.

    3. The computer will run out of memory if too many recursive calls are made.

  6. Which is the best characterization of the function chaos?

    1. it is recursive, and not iterative

    2. it is iterative, and not recursive

    3. it is both iterative and recursive

    These questions refer to Koch.py which generates a Koch Snowflake.

  7. Which is the best characterization of the function draw?

    1. it is recursive, and not iterative

    2. it is iterative, and not recursive

    3. it is both iterative and recursive

  8. In function draw if the value of parameter iters is 1, what will the length of string flake be after the first for-loop?

    1. 18

    2. 23

    3. 28

  9. If the value of iters is 4 what will the length of flake be?

    1. below 100

    2. between 100 and 500

    3. more than 500


Practice for Next Week's Midterm

  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):
          
          #code here
    
    
          close(f)