Previous | Home | Lecture Notes | Solutions | Next

 

Perl Exercises (8)


These exercises are part of a Perl course taught at CNTS - Language Technology Group at the University of Antwerp.

If you are a course participant and you want to submit your answers, please send the solutions to these exercises to erikt@uia.ua.ac.be before or on Wednesday March 29, 2000. Note that 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, if there are any.

Exercise 8.1

All the programs you construct for these exercises must contain the line use strict at the top.

Write two error checking routines for the texttool program. The first &checkError1() takes as argument a single number and returns a zero when the number of elements of the global list @args is equal to that number. Otherwise it returns a 1. The second &checkError2() takes as arguments an arbitrary list of numbers and returns a zero when for each of these numbers $i, the global hash entry $text{$args[$i]} is defined. Otherwise it returns a 2. Example:

   use strict; # oblige usage of my
   my @args = ("text1","text2");
   my %text = ("text1" => "small text\n");
   print &checkError1(2),"\n"; # prints 0
   print &checkError1(3),"\n"; # prints 1
   print &checkError2(0),"\n"; # prints 0
   print &checkError2(0,1),"\n"; # prints 2

Exercise 8.2

Write a program that reads a text that the user types in at the keyboard and prints each line in the text together with some context. You may assume that the user enters lines which contain one word each. Example:

   DOS> perl expand 2
   the
   top
   tip
   the
   tie
   tax
   ^Z
   ??? ??? the top tip
   ??? the top tip the
   the top tip the tie
   top tip the tie tax
   tip the tie tax ???
   the tie tax ??? ???

So the program shows the words entered by the user but additionally it shows n words before and n words behind each word. The value of n is specified as an argument of the program. The non-existent context words before the initial words and behind the final words are replaced by three question marks for each word.

Your program should use one subroutine for reading the text into a list containing one word per element and another subroutine for generating the output from that list.

Exercise 8.3

The Fibonacci sequence can be generated by a function f applied on non-negative integers which is defined as:

   f(0)=0
   f(1)=1
   f(n)=f(n-1)+f(n-2) for n>1

Write a recursive subroutine which takes a non-negative integer as input and returns the corresponding Fibonacci value. Example:

   use strict;
   print f(10),"\n"; # prints 55

Note: when you add something similar to the third line of the definition to your program, it will probably work but it will perform a lot of unnecessary computations. Try to come up with an efficient solution.

Exercise 8.4*

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

Write a simple hyphenation program. It should read a word presented by a user and show how the word should be hyphenated in your favorite language. Example:

   > caravan
   ca-ra-van
   > monster
   mon-ster
   > mat
   mat

In your description and the test of your program you should present both examples of correct and incorrect behavior of the program.

Exercise 8.5*

Write a program that reads a text and converts it to a version in which every syllable has been spelled backwards. This requires that you tokenize the text, recognize syllables and invert the syllables. For the first task, you can use the tokenize code presented in the answer of exercise 3.5* or your own code. For the second task, you can use the code you have written for exercise 8.4*. Example:

   DOS> cipher
   Can you accommodate the absent-minded professor?
   ^Z
   nac uoy camocometad eht batnes-nimded orpsefros?

Can your program be used for converting the text back to the original format?


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