Previous | Home | Lecture Notes | Exercises | Next

 

Perl Solutions (2)


These exercise solutions are part of a Perl course taught at CNTS - Computational Linguistics at the University of Antwerp.

Exercise 2.1

Write a program that reads a number and prints the table for that number three times. Each of the three tables should be coded with a different iterative structure.

My program can be found in the appendix. I have tested it with the number 12 and the program printed the following table three times:

    1 * 12 = 12
    2 * 12 = 24
    3 * 12 = 36
    4 * 12 = 48
    5 * 12 = 60
    6 * 12 = 72
    7 * 12 = 84
    8 * 12 = 96
    9 * 12 = 108
   10 * 12 = 120

Exercise 2.2

Write a program that reads an arbitrary number of non-negative numbers and stops when a negative number entered. After that the program should print how many numbers were entered and what the average of these numbers is.

My program can be found in the appendix. I have tested it with the numbers 17, 0, 5 and -1 as stop sign. The output of the program was:

   3 numbers were entered
   The average is 7.33333333333333

Exercise 2.3

Implement a mini expert system for determining whether people are rich or poor.

My program can be found in the appendix. Here is a test run:

   Please answer with 1 (yes) or 0 (no)
   Do you own a house? 0
   Do you own a big car? 0
   Do you own a small car? 0
   Do you own a grocery cart? 1
   Do you live on the street? 0
   You are a poor person.

Exercise 2.4*

Write a Perl program that reads five numbers and prints them twice: once in ascending order and once in descending order.

My program can be found in the appendix. It stores the numbers in five variables $number1 - $number5. At all times, it makes sure that the numbers are sorted in the order $number1 >= $number2 >= $number3 and so on for those variables that are defined. The variables will be filled in the order $number1, $number2 and so on. For example, if the fourth number is read, it will be compared with the other three. If it is larger than the third but smaller than the second then it should be inserted between those two. The third number moves to place four and the new number is put on place three. Here is a test run:

   Please enter number 1: 4
   Please enter number 2: 8
   Please enter number 3: 2
   Please enter number 4: 3
   Please enter number 5: 9
   Ascending order: 2 3 4 8 9
   Descending order: 9 8 4 3 2

Exercise 2.5*

Write a Perl program which solves the puzzle LEARN-PERL=HERE. Each character represents a number (0-9), different characters represent different numbers and H, L and P are not equal to zero.

I have chosen a brute-force approach: test every combination of characters and numbers. Since L can not be any number other that 1, I have not tested other values for that character. For all other characters I have tested the values 0 and 2-9 except for P and H for which I only tested 2-9. The program generated four solutions:

   12403 - 5201 = 7202
   12403 - 7201 = 5202
   13604 - 5301 = 8303
   13604 - 8301 = 5303

It can be found in the appendix.

Appendix: programs

This appendix contains the programs I have written for the exercises 2.1-2.5.

Exercise 2.1

# exercise21: print three tables
# usage:      exercise21
# 2000-02-14 erikt@uia.ua.ac.be

print "Enter a number: ";
$number = <>;
chomp($number);
# solution with while
$i = 1;
while ($i<10) { 
   print " $i * $number = ",$i*$number,"\n"; 
   $i++; 
}
print "10 * $number = ",10*$number,"\n";
# solution with for
for ($i=1;$i<10;$i++) { 
   print " $i * $number = ",$i*$number,"\n"; 
}
print "10 * $number = ",10*$number,"\n";
# solution with foreach
foreach $i (1,2,3,4,5,6,7,8,9) { 
   print " $i * $number = ",$i*$number,"\n"; 
}
print "10 * $number = ",10*$number,"\n";
exit(0);

Exercise 2.2

# exercise22: process list of numbers
# usage:      exercise22
# 2000-02-14 erikt@uia.ua.ac.be

# initialization
$sum = 0;
$counted = 0;
# read first number
print "Enter a number: ";
$number = <>;
chomp($number);
while ($number >= 0) {
   $counted++;
   $sum += $number;
   # read next number
   print "Enter a number: ";
   $number = <>;
   chomp($number);
}
# print results
print "$counted numbers were entered\n";
# avoid dividing by zero
if ($counted > 0) {
   print "The average is ",$sum/$counted,"\n";
}
exit(0);

Exercise 2.3

# exercise23: mini expert system
# usage:      exercise23
# 2000-02-14 erikt@uia.ua.ac.be

print "Please answer with 1 (yes) or 0 (no)\n";
print "Do you own a house? ";
$house = <>;
chomp($house);
print "Do you own a big car? ";
$bigCar = <>;
chomp($bigCar);
print "Do you own a small car? ";
$smallCar = <>;
chomp($smallCar);
print "Do you own a grocery cart? ";
$groceryCart = <>;
chomp($groceryCart);
print "Do you live on the street? ";
$street = <>;
chomp($street);

if ($house and $bigCar) { 
   print "You are a rich person.\n"; 
} elsif (not $house and not $bigCar and not $smallCar and
         ($groceryCart or $street)) {
   print "You are a poor person.\n";
} else {
   print "You are a middle-class person.\n";
}
exit(0);

Exercise 2.4*

# exercise24: sort five numbers
# usage:      exercise24
# 2000-02-14 erikt@uia.ua.ac.be

# read first number
print "Please enter number 1: ";
$number = <>;
chomp($number);
# the program stores the numbers in five variables 
# $number1 - $number5. It makes sure that at all 
# times $number1 >= $number2 >= $number3 and so on
$number1 = $number;

# read second number
print "Please enter number 2: ";
$number = <>;
chomp($number);
if ($number > $number1) { 
   $number2 = $number1;
   $number1 = $number;
} else {
   $number2 = $number;
}

# read third number
print "Please enter number 3: ";
$number = <>;
chomp($number);
if ($number > $number1) {
   $number3 = $number2;
   $number2 = $number1;
   $number1 = $number;
} elsif ($number > $number2) {
   $number3 = $number2;
   $number2 = $number;
} else {
   $number3 = $number;
}

# read fourth number
print "Please enter number 4: ";
$number = <>;
chomp($number);
if ($number > $number1) {
   $number4 = $number3;
   $number3 = $number2;
   $number2 = $number1;
   $number1 = $number;
} elsif ($number > $number2) {
   $number4 = $number3;
   $number3 = $number2;
   $number2 = $number;
} elsif ($number > $number3) {
   $number4 = $number3;
   $number3 = $number;
} else {
   $number4 = $number;
}

# read fifth number
print "Please enter number 5: ";
$number = <>;
chomp($number);
if ($number > $number1) {
   $number5 = $number4;
   $number4 = $number3;
   $number3 = $number2;
   $number2 = $number1;
   $number1 = $number;
} elsif ($number > $number2) {
   $number5 = $number4;
   $number4 = $number3;
   $number3 = $number2;
   $number2 = $number;
} elsif ($number > $number3) {
   $number5 = $number4;
   $number4 = $number3;
   $number3 = $number;
} elsif ($number > $number4) {
   $number5 = $number4;
   $number4 = $number;
} else {
   $number5 = $number;
}

print "Ascending order: ";
print "$number5 $number4 $number3 $number2 $number1\n";
print "Descending order: ";
print "$number1 $number2 $number3 $number4 $number5\n";
exit(0);

Exercise 2.5*

# exercise25: solve puzzle LEARN-PERL=HERE
# usage:      exercise25
# 2000-02-14 erikt@uia.ua.ac.be

# assume that L=1
$l = 1;
# E can be anything except 1
foreach $e (0,2,3,4,5,6,7,8,9) {
 # A can be anything except 1
 foreach $a (0,2,3,4,5,6,7,8,9) {
  # A cannot be the same as E
  if ($a != $e) {
   foreach $r (0,2,3,4,5,6,7,8,9) {
    # R cannot be the same as E or A
    if ($r != $e and $r !=$a) {
     foreach $n (0,2,3,4,5,6,7,8,9) {
      if ($n != $e and $n !=$a and $n !=$r) {
       foreach $p (2,3,4,5,6,7,8,9) {
        if ($p != $e and $p !=$a and $p !=$r and $p !=$n) {
         foreach $h (2,3,4,5,6,7,8,9) {
          if ($h != $e and $h !=$a and $h !=$r and 
              $h !=$n and $h !=$p) {
           $learn = 10000*$l+1000*$e+100*$a+10*$r+$n;
           $perl = 1000*$p+100*$e+10*$r+$l;
           $here = 1000*$h+100*$e+10*$r+$e;
           if ($learn-$perl == $here) {
              print "$learn - $perl = $here\n";
           }
          }
         }
        }
       }
      }
     }
    }
   }
  }
 }
}
exit(0);


Previous | Home | Lecture Notes | Exercises | Next
Last update: February 19, 2000. erikt@uia.ua.ac.be