Previous | Home | Lecture Notes | PDF slides | Next

 

Perl 2007: Exercises 3


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 September 25, 2007. Do not forget to test your programs. Include the test results in your weekly reports.

Exercise 3.1

Write a program that reads numbers between zero and ninety-nine (inclusive; one per line) and prints the number in digits. The program should stop when the user presses Enter without entering anything else. Example run:

   Please enter a number in characters: zero
   You have entered: 0
   Please enter a number in characters: eleven
   You have entered: 11
   Please enter a number in characters: ninety-nine
   You have entered: 99
   Please enter a number in characters:
   Thanks for using this program!

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

Exercise 3.2

Pig Latin is a variant of Latin in which each word is replaced by a variant. Two rules are used for creating the word variants:

  1. Move the initial consonants to the end of the words: trap becomes aptr and one does not change.
  2. Additionally put ay behind every word: trap becomes aptray; one becomes oneay.

Write a program which reads some lines of text until an empty line is read and then outputs the Pig Latin version of the text. The input may only consist of lower case characters (leave out capital characters and punctuation). Example:

   Enter a text, line by line. Finish with an empty line.
   > this text will be difficult to read
   > once it is translated to pig latin
   > 
   isthay exttay illway ebay ifficultday otay eadray
   onceay itay isay anslatedtray otay igpay atinlay

Exercise 3.3

Make regular expressions for the following string descriptions:

  1. Strings containing only a's (including the empty string)
  2. Strings containing only a's followed by only b's (including the empty string)
  3. Strings containing only a's and b's in any order (including the empty string)
  4. Strings containing neither a's nor b's
  5. Strings containing exactly three a's
  6. Strings containing an even number of a's (including the empty string)
  7. Strings containing exactly two words regardless of white space
  8. Strings containing one arbitrary character at least two times
  9. Strings containing a word ending in ing
  10. Strings containing a noninitial word starting with a capital character

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 plays a letter game. It generates questions of the format: What is the 3rd letter following f? The user has to respond with the correct answer (in this case i). The program should choose arbitrary letters itself as well as the relative positions (1-10, either before or following the current letter). After ten questions, the program should stop and give an overview of the user's performance. Example:

    1. What is the 3rd letter following f? 
       > i
       Correct!
    2. What is the 2nd letter before z? 
       > y
       Wrong.
       ...
   You have 7 correct answers on 10 questions.

[Hints]

Exercise 3.5*

Write a program that reads the plural of a word (you are free to use either English or Dutch) and prints the singular form. The program should process plurals until the user presses Enter without entering a word. Example run:

   Please enter a plural: boys
   The singular form is:  boy
   Please enter a plural: dishes
   The singular form is:  dish
   Please enter a plural: cherries
   The singular form is:  cherry
   Please enter a plural: wolves
   The singular form is:  wolf
   Please enter a plural: fish
   The singular form is:  fish 
   Please enter a plural: feet
   The singular form is:  foot
   Please enter a plural: attorneys general
   The singular form is:  attorney general
   Please enter a plural word:   
   Thanks for using this program!

It is too much work to achieve 100% coverage but try to handle a reasonable number of cases. Test your program with plurals that are handled correctly as well as with some that the program cannot process well.

[Hints]


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