Previous | Home | Lecture Notes | Solutions | Next

 

Perl Exercises (2)


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 17, 2000. Note that the only the first three exercises are obligatory. When you submit your results, please include the Perl code you have written, the result of at least one test and the answers to the questions mentioned in the exercise.

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. The tables should look like this example for input 17:

    1 * 17 = 17
    2 * 17 = 34
    3 * 17 = 51
    4 * 17 = 68
    5 * 17 = 85
    6 * 17 = 102
    7 * 17 = 119
    8 * 17 = 136
    9 * 17 = 153
   10 * 17 = 170

So all the multiplication operators and equal-to signs should be printed exactly under each other.

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.

Note: the negative number is used as a stop sign. It does not have to be included in the count and the average.

Exercise 2.3

Implement a mini expert system for determining whether people are rich or poor. It should ask the following questions:

  1. Do you own a house?
  2. Do you own a big car?
  3. Do you own a small car?
  4. Do you own a grocery cart?
  5. Do you live on the street?

The answers to the questions should be entered with 1 representing yes and 0 representing no. The program should respond with "You are a rich person." if both the first and the second question have been answered with yes. It should answer "You are a poor person." if the first three questions have been answered with no and one or two of the remaining questions have been answered with yes. In all other cases it should respond with "You are a middle-class person.".

Exercise 2.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.

Write a Perl program that reads five numbers and prints them twice: once in ascending order and once in descending order. For example, if the program receives 82, 0, 27, 9 and 3 as input then it should print:

Ascending order: 0 3 9 27 82
Descending order: 82 27 9 3 0

Note: the program may not use lists (these will be introduced in week four).

Exercise 2.5*

In 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. Furthermore the subtraction is correct. Write a Perl program that solves this puzzle. What solutions do you get?


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