8<--------------------------------------------------------------------------- #!/usr/bin/perl -w # # Exersize 2.16.4 solution. # # Raymond Fellers, Ph.D. # csci44a@yahoo.com # csci44a Perl I # Chabot College # @a = (1 .. 10); @b = (); for($i=0, $j=$#a; $i<@a; $i++, $j--) { $b[$j] = $a[$i]; } print "Array \@a contains: @a \n"; print "Array \@b contains: @b \n"; exit; 8<--------------------------------------------------------------------------- #!/usr/bin/perl -w # # Exersize 2.16.5 solution. # # Raymond Fellers, Ph.D. # csci44a@yahoo.com # csci44a Perl I # Chabot College # # Note: there's more than one way to solve this problem. # @a = (1 .. 10); @b = (); for($i=-1, $j=0; $i>=-@a; $i--, $j++) { $b[$j] = $a[$i]; } print "Array \@a contains: @a \n"; print "Array \@b contains: @b \n"; exit; 8<--------------------------------------------------------------------------- #!/usr/bin/perl -w # # Exersize 2.16.6 solution. # # Raymond Fellers, Ph.D. # csci44a@yahoo.com # csci44a Perl I # Chabot College # @a = (1 .. 10); @b = (); @b = reverse @a; print "Array \@a contains: @a \n"; print "Array \@b contains: @b \n"; exit; 8<--------------------------------------------------------------------------- #!/usr/bin/perl -w # # Exersize 2.16.7 solution. # # Raymond Fellers, Ph.D. # csci44a@yahoo.com # csci44a Perl I # Chabot College # # # Here's how you can solve the problem with push & pop. # @a = (1 .. 10); @b = (); while( $tmp = pop(@a) ) { push(@b,$tmp) } print "Solution using push & pop.\n"; print "Array \@a contains: @a \n"; print "Array \@b contains: @b \n"; # # Here's how you can solve the problem with shift & unshift. # @a = (1 .. 10); @b = (); while( $tmp = shift(@a) ) { unshift(@b,$tmp) } print "Solution using shift & unshift.\n"; print "Array \@a contains: @a \n"; print "Array \@b contains: @b \n"; exit; 8<--------------------------------------------------------------------------- #!/usr/bin/perl -w # # Number guessing game. # Exercise 3.11.1 solution. # # Raymond Fellers, Ph.D. # csci44a@yahoo.com # csci44a Perl I # Chabot College # $maxNumGuesses = 5; $continuePlaying = 'yes'; while( $continuePlaying eq 'yes' ) { # # Reinitialize our variables to start a new game. # print "Pick a number between 1 and 10 and I'll guess it.\n"; print "Press ENTER when ready.\n"; ; $timesGuessed = 0; @guessedNumbers = (); # delete previously guess numbers for this game # # This while loop runs until the computer uses up its allowed # guesses (5 max) or it guess the correct number. # while( $timesGuessed < $maxNumGuesses ) { # # The computer stores its guessed numbers in @guessedNumbers. # Each time it makes a guess, except the first time, it compares # the randomly generated number with the previous guesses stored # in @guessedNumbers. If the random number matches any number # number in @guessedNumbers, another random number is generated # and the computer checks again. We keep doing this until # the random number is not in @guessNumbers. This ensures the # computer will never repeat a guess. # if( @guessedNumbers ) { $badGuess = ''; # # Keep guessing until we don't have a bad guess. # until( $badGuess eq 'no' ) { $guess = int( rand(10) ) + 1; # rand number 1 - 10 $badGuess = 'no'; # # A guess is bad if its already in @guessedNumbers. # foreach (@guessedNumbers) { $badGuess = 'yes' if($guess == $_); } } } # # This is the first guess so the computer doesn't need to # check if this number is a repeat. # else{ $guess = int( rand(10) ) + 1; } # # Store the good guess in @guessedNumbers so the computer # doesn't guess it again. # push(@guessedNumbers,$guess); # # Ask the user if the computer guessed correctly. # print "Is your number $guess?\n"; chomp( $answer = ); if($answer eq 'yes') { print "Ha ha! I win!\n"; print "Do you want want to keep playing?\n"; chomp( $continuePlaying = ); last; # end the while loop immediately }else{ $timesGuessed++; # the computer used up one of it's guesses } } # # The computer used up all of it's guesses and so loses the game. # Ask the user if they want to play again. The user must type in # 'yes' if they want to play again. # if( $timesGuessed == $maxNumGuesses ) { print "Darn! I ran out of guesses.\n"; print "Do you want want to keep playing?\n"; chomp( $continuePlaying = ); } } print "Bye bye.\n"; exit;