Home | Lecture Notes | Exercises | Next

 

Perl Solutions (1)


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

Exercise 1.1

Yes, the program behaves as could have been expected.

Exercise 1.2

I have replaced the print line of the program with:

   print "Your age is ",2000-$yearOfBirth-1;
   print " or ",2000-$yearOfBirth,".\n";

I have tested the program with input 1976 and the result was:

   Your age is 23 or 24.

This is correct.

Exercise 1.3

My solution is:

   # exercise13: program for exercise1.3 of Perl course
   # 2000-02-04 erikt@uia.ua.ac.be

   print "Enter non-zero digit: ";
   $number = <>;
   $number = $number+1;
   $number = $number*9;
   $number = int($number/10)+$number%10;
   $number = $number*262;
   $number = $number-27;
   $number = $number/3;
   print "The result is ",$number,"\n";

I have tested the program with the input digits 1, 2 and 3 and in all cases the result was 777.

Exercise 1.4*

My solution is:

   # exercise14: program for exercise1.4 of Perl course
   # 2000-02-04 erikt@uia.ua.ac.be

   print "Enter non-zero digit: ";
   $digit1 = <>;
   print "Enter non-zero digit: ";
   $digit2 = <>;
   print "Enter non-zero digit: ";
   $digit3 = <>;

   $number1 = 10*$digit1+$digit2;
   $number2 = 10*$digit1+$digit3;
   $number3 = 10*$digit2+$digit1;
   $number4 = 10*$digit2+$digit3;
   $number5 = 10*$digit3+$digit1;
   $number6 = 10*$digit3+$digit2;

   $sum1 = $number1+$number2+$number3+$number4+$number5+$number6;
   $sum2 = $digit1+$digit2+$digit3;

   print "The result is ",$sum1/$sum2,"\n";

I have tested this program with three sets of digits: {1,2,3}, {3,6,9} and {8,8,8}. In all three cases the result was 22.

Exercise 1.5*

I have only changed the part dealing with February (two lines) and the part adjusting for the first of January being a Monday (two lines). The four lines involved are now:

   print "Is the day after February 28? ";
   $dateNbr = $dateNbr+$answer*28;
   # adjust for January 1 being a Monday
   $dateNbr = $dateNbr+6;

I have tested the program for January 1 and December 12 and in both cases the answers (0,2) were correct.


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