Homework 2. Stat 540 Spring 1999.

Have the programs written for parts 1 and 3 of this homework available through a CGI form from a file called homework02.html, in a directory called homework02.

1. Write a Perl script to repeatedly throw 5 dice until all 5 dice show the same number. Have the program count the number of iterations until this event occurs. Embed this code in a for loop so that the simulation is repeated 1000 times. Have your program report the empirical probability of rolling 5 dice and obtaining the same number on all 5.

2. Modify your subroutine that rolls the dice, so that it can be used to draw a simple random sample with replacement of 4 colors from 6 colors (label the colors R (red), Y (yellow), G (green), P (purple), L (lemon) and S (silver). In essence your subroutine should accept 2 pieces of information; the set of objects that constitute the population and the number of objects to be drawn, and return the simple random sample.

3. Write a subroutine that takes the random sample of colors and displays them as a row in an HTML table. If the colors were (R, Y, G, P) then the HTML code that your script needs to output is:

<TABLE> <TR>
<TD> R <TD> Y <TD> G <TD> P
</TR></TABLE>
(Don't forget to output the HTTP header as well).

Make the subroutine accessible over the WEB so that I can produce the random table. (Brownie points available for figuring out how to color the table itself.)

4. Write a subroutine that compares 2 lists; a code list and a guess at the code. Your subroutine should return 2 numbers, the number of exact matches and the number of colors that are correct but in the wrong place. The code list should be of length 4 and composed of the 6 possible colors, as should the "guess list". Include this subroutine in a file called "compare.pl" in your cgi-bin directory. Make sure that this script is both readable and executable by the world.

It will be a really good idea to write out your algorithm before programming it.

As test cases the following codes and guesses should return the following 2 numbers.

                          Correct color and      Correct color but
CODE       GUESS          in the right place.    wrong place.

R G Y L    R R R R        1                      0

R R Y L    L Y R L        1                      2

G Y L L    L L G Y        0                      4