Previous | Home | Lecture Notes | Solutions | Next

 

Perl Exercises (4)


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 zavrel@uia.ua.ac.be before or on Wednesday March 1st, 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, if needed, the answers to the questions mentioned in the exercises.

Exercise 4.1

Write a program that reads one line with integer numbers and prints the square of all the even numbers greater than 10.

Input: 1 12 6 8 20 71 100
Output: 144 400 10000

Exercise 4.2

Write a program that reads lines from standard input until end-of-file, then prints the number of lines, words and characters in the input, followed by the input in reverse order (both lines and characters).

Nay, but this dotage of our general's
O'erflows the measure: those his goodly eyes,
That o'er the files and musters of the war
Have glow'd like plated Mars, now bend, now turn,
The office and devotion of their view
Upon a tawny front: his captain's heart,
Which in the scuffles of great fights hath burst
The buckles on his breast, reneges all temper,
And is become the bellows and the fan
To cool a gipsy's lust.
Gives the output:
lines: 10, words: 76, characters 404 
reversed:
.tsul s'yspig a looc oT
naf eht dna swolleb eht emoceb si dnA
,repmet lla segener ,tsaerb sih no selkcub ehT
tsrub htah sthgif taerg fo selffucs eht ni hcihW
,traeh s'niatpac sih :tnorf ynwat a nopU
weiv rieht fo noitoved dna eciffo ehT
,nrut won ,dneb won ,sraM detalp ekil d'wolg evaH
raw eht fo sretsum dna selif eht re'o tahT
,seye yldoog sih esoht :erusaem eht swolfre'O
s'lareneg ruo fo egatod siht tub ,yaN

Exercise 4.3

Write a program that recognizes palindromes (words or phrases that read the same in both directions). Deal with one-letter words, and be permissive with whitespace and punctuation, so that the program behaves as specified by the example below.

Type a word or phrase: computer
Not a palindrome.
Type a word or phrase: level
This is a palindrome.
Type a word or phrase:  Redivider
This is a palindrome.
Type a word or phrase:  i
One letter palindrome, trivial!
Type a word or phrase: Sums are not set as a test on Erasmus
This is a palindrome.
Type a word or phrase: Satan, oscillate my metallic sonatas!
This is a palindrome.

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

A letter bigram is a sequence of two adjacent letters. E.g. "bigram" contains the bigrams "bi ig gr ra am". We do not count bigrams containing whitespace. Write a program that reads text from standard input until end-of-file, and then prints the frequency of each bigram that occurs in the text. You may not use hashes (as these will be the subject of next weeks lecture).

Input: this is the bigram count!
Output:
th  2
hi  1
is  2
he  1 
bi  1
ig  1
gr  1
ra  1
am  1
co  1
ou  1
un  1
nt  1
Hint: give each bigram an index number in an array.


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