Compsci 06/101, Spring 2011, Lab 11

By entering your name/net-id below you indicate you are present for Lab 11 to answer these questions and that you were part of the process that resulted in answers being turned in.
Name: ______________    Net id: _____________ || Name: ______________    Net id: _____________

Name: ______________    Net id: _____________ || Name: ______________    Net id: _____________

  1. User-generated substitutions

    1. in do_mad_lib what is the purpose of this code in the function (you can remove it to see what it does)? contents = re.sub("\n", " ", contents)

    2. What about the statement above allows you to infer that re.sub does not change its argument contents, i.e., that the function re.sub returns a value but doesn't have a side-effect on its parameter?
      
      
      
    3. What is the list-comprehension you used to substitute values generated by calling get_word_from_user for each element in all_tags?
      
         subs = 
      

  2. Using the string format operator %

    1. To make the string formatting operator work you added this line story = template % tuple(subs)

      For that line to work, the string template must have an occurrence of the string "%s" for every corresponding replacement found in the list subs. What line of code that was provided in do_mad_lib caused the string "%s" to replace each tag, e.g., "<adjective>" in the string read from the file template_name? Why do you think it's this line?

      
      
      
    2. The regex pattern used is "<([^>]+)>" where the parentheses are used to create a group, the group whose index is 1 when the regular expression matches text. When the text "the <adjective> house" matches the pattern the entire match is "<adjective>" but the group(1) match is "adjective" without the angle brackets. What code are you given that uses the match without the angle brackets and why do you think the angle brackets are removed?
      
      
      
    3. In the regex pattern "<([^>]+)>" the + means one or more occurrences, in this case one or more occurrences of any character that IS NOT a greater than symbol, that's what the [^>] means. If a * is used instead of a + does the code still work? The asterisk means zero or more occurrences.
      
      
      
      
  3. Reading the Dictionary

    1. The read_tagstore function loops over each line that it reads, what is the purpose of calling line.strip()?
      
      
      

    2. What code creates a list of the comma-separated words that come after the colon on a line?
      
      
      
      

    3. The key in the dictionary is given by local variable key. What code did you write to add values to an already existing dictionary value (a list) corresponding to key?
      
      
      
      
  4. Word-getter Function parameter

    1. What is the list-comprehension that generates replacements for each tag in all_tags, but generates them automatically by calling get_word_from_computer?
      
      
      
      
    2. How many parameters does do_mad_lib have when you've finished this section?
      
      
      
  5. Function print_out

    1. What update did you make to curr_len if the line was too long? Why did you do this?
      
      
      
      
    2. What update to curr_len did you make when printing the word followed by a space?
      
      
      
    3. Why is the change print word, sufficient to print a word, followed by a space, and remain on the same line for the next print?
      
      
      
  6. tag-a-story category

    What is the category of the tag-a-story, Mad Lib you created?