Previous | Home | Lecture Notes | Solutions | Next

 

Perl Exercises (3)


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 24, 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 3.1

Rot13 is an encryption method in which each of the 26 characters a-z is replaced by the character 13 places ahead or 13 places back in the alphabet. Write a program that reads a texts and outputs the rot13 equivalent. Example:

   Enter a text, line by line. Finish with an empty line.
   > This text should be impossible to read
   > because it has been encrypted with a fine
   > encryption method.
   > 
   Guvf grkg fubhyq or vzcbffvoyr gb ernq
   orpnhfr vg unf orra rapelcgrq jvgu n svar
   rapelcgvba zrgubq.

Note that since rot13 is symmetric, the same program can be used both for encoding and decoding.

Exercise 3.2

Write a program that translates English text to text in some other language. It should at least be able to generate something reasonable for the words the, man, woman, boy, girl, telescope, sees, walks and with regardless of the context in which these words appear. Example:

   Enter a text, line by line. Finish with an empty line.
   > The man sees the boy.
   > The woman walks with the girl
   > 
   de man ziet de jongen.
   de vrouw loopt met het meisje.

The program may not use lists.

Exercise 3.3

Make regular expressions for the following string descriptions:

  1. Strings containing only a's and b's.
  2. Strings that do not contain white space.
  3. Strings with exactly one word regardless of white space.
  4. Strings that end with the same character they start with.
  5. Like 1. but the number of a's should be even.
  6. Any string.
  7. No string, not even the empty string.

You can test the expressions by putting them in a small program which tests whether an input string matches the expression or not.

Exercise 3.4*

Write a program that asks ten questions, reads the answers and shows the number of answers that were right. The questions should be like: Please enter n things in which n is an arbitrary number from 1 to 10 and things is one of the three words dollars, stars or commas. The questions should be chosen arbitrarily by the program. Example:

    1. Please enter 3 dollars: $$$
       Correct!
    2. Please enter 7 stars: ******
       Wrong.
       ...
   You have 7 correct answers on 10 questions.

Hint: Use the function rand.

Exercise 3.5*

Write a word tokenizer: a program which reads a text and outputs its words in the order that they appear in the text. Example:

   Enter a text, line by line. Finish with an empty line.
   > In a drive-in I saw R2D2(TM). Funny, isn't it?
   > 
   In
   a
   drive-in
   I
   saw
   R2D2
   TM
   Funny
   isn't
   it

Test your program with as many problematic cases as you can think of.


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