Difference between revisions of "Exercise 7.1"

From Earlham CS Department
Jump to navigation Jump to search
(New page: Example 7-1 (stored b/c could not be saved) <pre> #!/usr/bin/perl -w use strict; #Erika Phelps #Sept 30, 2009 #Ex 7-1: Children's game with random numbers #Declare the variables my $cou...)
 
Line 2: Line 2:
  
 
<pre>
 
<pre>
#!/usr/bin/perl -w
+
#!/usr/bin/perl
 +
use warnings;
 
use strict;
 
use strict;
  
 
#Erika Phelps
 
#Erika Phelps
#Sept 30, 2009
+
#Oct 4, 2009
#Ex 7-1: Children's game with random numbers
+
#Exercise 7.1
  
#Declare the variables
+
#Pseudocode:
my $count;
+
#Ask user to input an amino acid.
my $input;
+
#Guess a random nucleotide until the user confirms the guess.
my $number;
+
#From an array with the 4 nucleotides, randomly choose one to output
my $sentence;
 
my $story;
 
  
#Here are the arrays of parts of sentences
+
#Declare variables
  
my @nouns = (
+
my $amino_acid;
 
+
my $guess;
    'Dad', 'TV', 'Mom', 'Groucho', 'Rebecca', 'Harpo', 'Robin Hood',
+
my @nucleotide = ('A', 'T', 'G', 'C');
    'Joe and Moe',
+
my $nucleotide;
    );
+
my $input;
  
my @verbs = (
+
#Ask user to choose a nucleotide:
  
    'ran to', 'giggled with', 'put hot sauce into the orange juice of',
+
print "This program will try to guess which nucleotide you have chosen.\n";
    'exploded', 'dissolved', 'sang stupid songs with', 'jumped with',
+
print "Please input the one-letter code for an amino acid:\n";
    );
 
  
my @prepositions = (
+
$amino_acid = <STDIN>;
  
    'at the store', 'over the rainbow', 'just for the fun of it',
+
chomp $amino_acid;
    'at the beach', 'in New York City', 'in a dream', 'around the world',
 
    );
 
  
 
#Seed the random number generator
 
#Seed the random number generator
  
 
srand(time|$$);
 
srand(time|$$);
 
#This do-until loop composes six sentence "stories" until the user types quit.
 
  
 
do {
 
do {
  
     #(Re)set $story to the empty string each time through the loop
+
     $guess = $nucleotide[int(rand(scalar @nucleotide))];
 
 
    $story = '';
 
 
 
    #Make six sentences per story
 
 
 
    for ($count = 0, $count <6; $count++) {
 
 
 
        #Notes on the following statements:
 
        #1) scalar @array gives the number of elements in the array.
 
        #2) rand returns a random number greater than 0 and less than
 
            #scalar value
 
        #3) int removes the fractional part of a number
 
        #4) . joins two strings together
 
 
 
        $sentence = $nouns[int(rand(scalar @nouns))]
 
            . ""
 
            . $verbs[int(rand(scalar @verbs))]
 
            . ""
 
            . $nouns[int(rand(scalar @nouns))]
 
            . ""
 
            . $prepositions[int(rand(scalar @prepositions))]
 
            . '.';
 
 
 
        $story .= $sentence;
 
    }
 
  
    #Print the story
+
     print "Is \"$guess\" your nucleotide? Type \"Y\" if yes and \"N\" if no.\n";
 
 
     print "\n",$story,"\n";
 
 
 
    #Get user input.
 
 
 
    print "\nType \"quit\" to quit, or press Enter to continue: ";
 
  
 
     $input = <STDIN>;
 
     $input = <STDIN>;
  
    #Exit the loop at user's request
+
} until($input =~ /^s*y/i);
 
 
} until($input =~ /^\s*q/i);
 
  
 
exit;
 
exit;
 +
   
 
</pre>
 
</pre>

Revision as of 23:30, 4 October 2009

Example 7-1 (stored b/c could not be saved)

#!/usr/bin/perl
use warnings;
use strict;

#Erika Phelps
#Oct 4, 2009
#Exercise 7.1

#Pseudocode:
#Ask user to input an amino acid.
#Guess a random nucleotide until the user confirms the guess.
#From an array with the 4 nucleotides, randomly choose one to output

#Declare variables

my $amino_acid;
my $guess;
my @nucleotide = ('A', 'T', 'G', 'C');
my $nucleotide;
my $input;

#Ask user to choose a nucleotide:

print "This program will try to guess which nucleotide you have chosen.\n";
print "Please input the one-letter code for an amino acid:\n";

$amino_acid = <STDIN>;

chomp $amino_acid;

#Seed the random number generator

srand(time|$$);

do {

    $guess = $nucleotide[int(rand(scalar @nucleotide))];

    print "Is \"$guess\" your nucleotide? Type \"Y\" if yes and \"N\" if no.\n";

    $input = <STDIN>;

} until($input =~ /^s*y/i);

exit;