Difference between revisions of "Exercise 7.1"
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...) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | Return to [[Week 4]] | |
+ | <br> | ||
+ | Exercise 7.1 in <i>Beginning Perl for Bioinformatics</i> | ||
<pre> | <pre> | ||
− | #!/usr/bin/perl | + | #!/usr/bin/perl |
+ | use warnings; | ||
use strict; | use strict; | ||
#Erika Phelps | #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 @ | + | 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 | #Seed the random number generator | ||
srand(time|$$); | srand(time|$$); | ||
− | |||
− | |||
do { | do { | ||
− | + | $guess = $nucleotide[int(rand(scalar @nucleotide))]; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | print "Is \"$guess\" your nucleotide? Type \"Y\" if yes and \"N\" if no.\n"; | |
− | |||
− | print "\ | ||
− | |||
− | |||
− | |||
− | |||
$input = <STDIN>; | $input = <STDIN>; | ||
− | + | } until($input =~ /^s*y/i); | |
− | |||
− | } until($input =~ /^ | ||
exit; | exit; | ||
+ | |||
</pre> | </pre> |
Latest revision as of 22:31, 4 October 2009
Return to Week 4
Exercise 7.1 in Beginning Perl for Bioinformatics
#!/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;