Biol 591 Introduction to Bioinformatics (Fall 2002)

Programming Problem Set Intro

QIP.1. Here is a small piece of Perl, similar to the core of the bottles.pl program:
my $number = 10;
while ($number > 0) {
   Do something with the $number;
   $number = $number - 1;
}
What is the first number that we do something with? What is the last number? Hint: replace do something with a line that prints the number. Then run the small program that results.

QIP.2. Change the program you wrote in question 1 to count up instead of down.

QIP.3. The DiceRoll.pl program contains a comment:

# random_integer($low, $high) returns a random integer between $low and $high
#    inclusive.
# Assumption: $low < $high, and both are integers.
followed by the random_integer routine that the comment mentions.
sub random_integer {
  my ($low, $high) = @_;
  my $range = $high - $low + 1;
  my $random_fraction_of_range = rand($range);
  return $low + int($random_fraction_of_range);
}
Such comments are often useful when you're trying to modify a program -- they tell you what a section of the program does, so that you don't have to decipher it line by line.

On the other hand, comments aren't always truthful -- perhaps the author was mistaken, or perhaps the program was modified but the comment wasn't. So we sometimes need to test the truth of a comment.

Write a program that will test random_integer -- something that will increase (or utterly destroy) your confidence that the line

my $die_value = random_integer(1,6);
will always set $die_value to an integer between 1 and 6 - never 0 or -23 or 1024 or 3.14159.

This program doesn't need to be elaborate -- perhaps it will simply print out ten trials of random_integer.

For your convenience, click on random_integer.pl for a file with the random_integer section of the code already typed in.

QIP.4. If you have programming experience you might enjoy writing a more elaborate test program - you could automatically check 10 million trials of random_integer. That's not the exercise, though.

The exercise is to answer these questions:

Assume that random_integer passes the 10-trial test. Your confidence in the comment will increase somewhat. Then you run the 10-million-trial test. If random_integer passes this test, will your confidence increase a million times as much? Why or why not?
QIP.5. The comment says:
# Assumption: $low < $high, and both are integers
In other words, the author doesn't guarantee that random_integer($low, $high) will return a random integer between $low and $high inclusive unless $low is less than $high and both are integers. Is this too conservative? What happens if $low or $high are not integers? If $low equals $high? If $low is less than $high?

QIP.6. Take out int in the last line of random_integer, so that it looks like this:

sub random_integer {
  my ($low, $high) = @_;
  my $range = $high - $low + 1;
  my $random_fraction_of_range = rand($range);
  return $low + $random_fraction_of_range;
}
Run your 10-trial test. What does int do?

QIP.7. Now take out rand in the next to last line:

sub random_integer {
  my ($low, $high) = @_;
  my $range = $high - $low + 1;
  my $random_fraction_of_range = $range;
  return $low + $random_fraction_of_range;
}
What does rand do?

QIP.8. By fooling around with some working program, determine when upper case/lower case makes a difference in the operation or results of a Perl program.