Home | Lecture Notes | Solutions | Next

 

Perl Exercises (1)


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

If you are a course participant, please send the solutions to these exercises to erikt@uia.ua.ac.be before or on Wednesday February 9, 2000. Note that the only the first three exercises are obligatory.

Exercise 1.1

In the lecture notes we put forward the following age calculator:

   # age calculator
   print "Please enter your birth year ";
   $yearOfBirth = <>;
   chomp($yearOfBirth);
   print "Your age is ",2000-$yearOfBirth,".\n";

Use the editor for storing this program in a file and run it. Does it behave as you expected?

Exercise 1.2

The program of exercise 1.1 contains a problem: it will only show the correct age if the user has already celebrated his birthday this year. Modify the program so that it produces output like:

   Your age is 23 or 24.

One of the two numbers should represent the correct age.

Exercise 1.3

Implement the following instructions in a Perl program. All parts should be represented by a command in your program.

Take a non-zero digit (1-9). Add one to this number. Multiply it with nine. Add the digits of the resulting two-digit number to each other. Multiply the result with 262. Subtract 27 from this and divide the result by 3.

The program should read an arbitrary number, perform the instructions and print the result. Test your program on three different numbers. What results do you obtain?

Exercise 1.4*

This is a starred exercise which means that you may skip the exercise. Make this exercise only if you think it is interesting and you have some time left.

Implement the following instructions in a Perl program. All parts should be represented by a command in your program.

Take three non-zero digits. Create six two-digit numbers from these digits and add these together. Now add the original three digits together and divide the previous result by this sum.

Test your program with three different sets of digits. What results do you obtain?

Note: there are nine different combinations of three digits. The combinations which you should use are the combinations of digits with other digits.

Exercise 1.5*

Modify the weekday calculator presented in section 1.4 of the lecture notes in such a way that it correctly handles dates from the year 2001 instead of 2000.


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