Compsci 06, Spring 2011, Grammar FUN, April 6

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