Introduction to Computer Science
CompSci 101 : Spring 2014

Form Letters

Ever received one of those annoying, "personalized", junk (e)mail? Your task today is to generate them! These mass personalized form letters can be easily created using programs like Microsoft Word's mail merge feature. All it takes is a specially formatted document which contains a template of the text to be sent with embedded references, which act as placeholders to be replaced by text from a separate source of user data (typically a spreadsheet or a database which has a field or column for each variable in the template).

When a mail merge is run, the output document is created by combining the values in a row of user data with the template text, i.e., substituting the data references in the template with the values from the matching fields. This general approach is used in a wide variety of other applications, from generating dynamic web pages to making schedules to showing results for Mad Libs games.

Spam email is an excellent example of how the computer's ability to easily scale this task to produce millions of letters creates business opportunities but mostly problems for regular email users as well as legal complications. Now that your gadgets are joining the Internet, might they be targeted to send spam as well?

The goal of this lab is to using a basic string functions and loops to create any number of personalized form letters from a given template with data from a list of user information.

Start by snarfing the lab code from the course website (alternatively, you can browse code here):

http://www.cs.duke.edu/courses/compsci101/spring14/snarf/

Here is an example form letter template. In the template, values to be replaced are denoted by a dollar sign, $, followed by a number. The number can be used as an index into a list of data values to retrieve specific data about each recipient.

Dear $1,

On behalf of $4, I would like to thank you for your generous gift of
$$3. The $5 is well on its way toward
its goal of one-thousand dollars.

The support shown by all those who donated has been phenomenal. We
will be contacting you in the Spring to let you know when the
dedication ceremony will take place.

Again, thank you $1 $2.

Here is the information you might have on file of people that have donated to your cause. Each person is represented by a single string, with their information fields separated by a colon, :. In this example, each string will have six fields: email address, first name, last name, amount contributed, foundation name, and cause for the contribution.

rr@cs.duke.edu:Ramblin:Roger:200:World Peace:Awesome Algorithm Trophy fund

Combining the template above with the example data (after it has been turned into a list using the function split(':')), produces the following personalized letter:

Dear Ramblin,

    On behalf of World Peace, I would like to thank you for your generous gift of
$200. The Awesome Algorithm Trophy fund is well on its way toward 
its goal of one-thousand dollars.

    The support shown by all those who donated has been phenomenal. We
will be contacting you in the Spring to let you know when the dedication
ceremony will take place.

    Again, thank you Ramblin Roger.

Where $1 was replaced by the second data field, the person's first name, Ramblin; $3 was replaced by the fourth data field, the contribution amount, 200; and so on. Note, the first data field, the email address, was not used in this example.

Specifications

Complete the following functions.

  1. fillInTemplate

    This function uses the template and a list of data values for one specific user to replace the template's generic references with the corresponding data string. It needs to do this for all of the data values in the list. For example, it should replace every occurrence of the substring '$1' with the string at index 1 in the list.

  2. printText

    This function prints a personalized letter from the given template for every item in the list of user data. Since the user data is given as a formatted string, where each field is separated by a colon, :, you will first need to convert each user string into a list that can passed to the function, fillInTemplate, to generate a letter that can be printed to the console. Thus, if the list contains data about ten users, this function should print to the console ten personalized letters.

  3. totalContributionBy

    This function shows that you can do other things with the data than simply fill in the references in the letter. It should use the format of the user's data (i.e., that the email address is the first field and the contribution amount is the fourth field) to calculate something interesting about the donations made in this example.

    Thus it should calculate the percentage of the total contributions made by people whose email address ends with the same string as the given domain.