Previous | Home | Lecture Notes | PDF slides | Next

 

Perl 2007: Exercises 5


These exercises are part of a programming course taught at University of Tilburg, The Netherlands.

If you are a course participant, please send the solutions to these exercises to erikt(at)science.uva.nl before or on Tuesday October 9, 2007. Do not forget to test your programs. Include the test results in your weekly reports.

Exercise 5.1

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

Write a program that reads an arbitrary list of numbers, one at a time (one per line), and stops when nothing is entered. The program should print how many numbers were entered and what the median of these numbers is.

The program should contain at least two subroutines: (1) one that reads the numbers and returns them in a list (no arguments); and (2) one that takes the list of numbers as arguments and returns the median. If an even number of numbers is entered, the program should return the average of the two center numbers. Example runs:

   $ perl -w 5.1.pl
   Please enter some numbers and finish with an empty line:
   > 3
   > 1
   > 2
   > 
   The median number of these 3 numbers is: 2
   $ perl -w 5.1.pl
   Please enter some numbers and finish with an empty line:
   > 1
   > 2
   > 
   The median number of these 2 numbers is: 1.5

[Solution] Only look at the solution when you have finished the exercise or when you are completely stuck.

Exercise 5.2

A concordance is a list of words with their context as used in a text. Write a program that (1) takes one word as argument; (2) reads a text; and (3) prints all occurrences of the word in the text preceded by the previous three words and the next three words.

The results should be printed in the same order as the appearance of the words in the text and the search words should be right under each other. The program should contain at least two subroutines: (1) one for reading the text until the first empty line (zero arguments); and (2) one for printing the result (two arguments: the search word and the text). Example run:

   $ perl -w 5.2.pl the
   Please enter a text:
   > Renowned curator Jacques Saunière staggered through the
   > vaulted archway of the museum's Grand Gallery. He
   > lunged for the nearest painting he could see, a Caravaggio.
   > Grabbing the gilded frame, the seventy-six-year-old man
   > heaved the masterpiece toward himself until it tore from the wall and
   > Saunière collapsed backward in a heap beneath the canvas.
   >
   These are the occurrences of "the" in the text:
        Saunière staggered through the vaulted archway of
                vaulted archway of the museum's Grand Gallery.
                     He lunged for the nearest painting he
            A Caravaggio. Grabbing the gilded frame, the
                 the gilded frame, the seventy-six-year-old man heaved
   seventy-six-year-old man heaved the masterpiece toward himself
                      it tore from the wall and Saunière
                    a heap beneath the canvas.

Exercise 5.3

A collocation is a set of words which occur together more often than by chance. Write a program that (1) takes a word W and a number N as argument; (2) reads a text; (3) collects the words that appear within N words of W in the text; and (4) print the ten most frequent words with their counts.

The program should contain at least three subroutines: (1) one for reading the text until the first empty line (zero arguments); (2) one for collecting the context words (three arguments: the search word W, the maximum distance N and the text); and (3) one for showing the result. In the text, you may ignore all the non-word characters ([^\w\s]), such as punctuation tokens. The output order for words that have the same frequency, is free. Example run:

   $ perl -w 5.3.pl the 3
   Please enter a text:
   > Renowned curator Jacques Saunière staggered through the
   > vaulted archway of the museum's Grand Gallery. He
   > lunged for the nearest painting he could see, a Caravaggio.
   > Grabbing the gilded frame, the seventy-six-year-old man
   > heaved the masterpiece toward himself until it tore from the wall and
   > Saunière collapsed backward in a heap beneath the canvas.
   >
   The top 10 words within a context of 3 to "the":
    1. 2 Saunière
    2. 1 seventy-six-year-old
    3. 1 vaulted
    4. 1 heaved
    5. 1 gilded
    6. 1 archway
    7. 1 frame
    8. 1 wall
    9. 1 man
   10. 1 of

Hint: the sorted list of hash keys required for printing, can be obtained with the instructions: sort { $hash{$b} <=> $hash{$a} } keys %hash . These sort the keys of the hash based on the hash values associated with each key in large-small order (switching $b and $a would result in small-large order).

Exercise 5.4*

The Padovan sequence is a sequence of integer values which satisfy the following constraints:

P(0) = P(1) = P(2) = 1
P(n) = P(n-2) + P(n-3) for N>2

Write a program that reads a number N from its arguments and prints the Nth Padovan number (note that index values start at 0). The computation of the Nth Padovan number should be implemented with a single recursive subroutine. Example run:

   $ perl -w 5.4.pl 10
   The 10th Padovan number is: 12

[Hints]

Exercise 5.5*

A spelling correction program both discovers misspelled words and suggests alternatives for the misspellings. Write a program with a small dictionary (10-20 words) that suggests words from its dictionary as alternatives for words that it does not know. The program should contain at least one subroutine which generates an alternative for a word outside of the dictionary (one argument: misspelled word).

The program should be able to deal with three basic errors: (1) insertion of a single extra character in the word (the becomes thhe); (2) deletion of a single character from the word (the becomes te); and (3) transposition: switching two characters within a word (the becomes teh. It may ignore non-word characters ([^\w\s]) and substitute unresolvable words with ???. The misspelled words may not be part of the program. Example run:

   $ perl -w 5.5.pl
   Please enter a line of text:
   > ni they ear 1878 Ik toko myn dergee fo oDctor fo Medcine
   I think you meant:
   in the year ??? I took my degree of Doctor of Medicine

[Hints]


Previous | Home | Lecture Notes | PDF slides | Next
Last update: October 23, 2007. erikt(at)science.uva.nl