Previous | Home | Lecture Notes | PDF slides | Next

 

Perl 2007: Exercises 4


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

Exercise 4.1

Anagrams are phrases that contain exactly the same characters. Write a program that tests if two phrases are anagrams of each other. You may ignore white space and capitalization. Example run:

   Please enter the first phrase:  tea
   Please enter the second phrase: tea
   These phrases are identical
   Please enter the first phrase:  ate
   Please enter the second phrase: tea
   These phrases are anagrams
   Please enter the first phrase:  something
   Please enter the second phrase: this gnome
   These phrases are anagrams
   Please enter the first phrase:  man
   Please enter the second phrase: manna
   These phrases are not related
   Please enter the first phrase:  
   Thank you for using this program!

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

Exercise 4.2

A part-of-speech tagger is a program that assigns word classes like Noun, Verb, Adjective, Adverb, Pronoun, Preposition, Conjunction and Determiner to words. Write a part-of-speech tagger for your favorite language. The program should read a text and assign part-of-speech tags to the words in the text based on lexical information that it keeps in a hash. Example run:

   Please enter a sentence:     several thousand monks in Burma set off 
                                on a new march
   This is the tagged sentence: several/Adj thousand/Unknown monks/Noun 
                                in/Prep Burma/Noun set/Verb off/Prep 
                                on/Prep a/Det new/Adj march/Noun

You may assume that words are unambiguous, so that they always receive the same unique tag. In the output, part-of-speech tags may be abbreviated with three or four characters. A hash containing twenty different words with part-of-speech tags is sufficient. Assign tag Unknown to all other words.

Exercise 4.3

A bigram model is a model that uses statistics of occurrences of pairs of tokens. Write a program that constructs a bigram character model from an input text and prints the model. The program may ignore the difference between upper and lower case characters and may ignore all the characters that are outside {a-z,A-Z,whitespace}. Assume that lines start and end with a space and reduce different white space characters (" ","\t","\n") to spaces. The bigram model should consist of counts of character pairs. Example run:

   Enter a text and finish with an empty line:
   This is my test text.
   
   Bigram model:
    i 1
    m 1
    t 3
   es 1
   ex 1
   hi 1
   is 2
   my 1
   s  2
   st 1
   t  2
   te 2
   th 1
   xt 1
   y  1

Ignore the colors in the output. These are clues for exercise 4.4*.

Exercise 4.4*

Write a program that generates a restricted amount of text according to a bigram model. The program should start with reading an example text and building a bigram model (exercise 4.3), ask for a number of characters to generate and then generate this number of characters. The output sequence should be random but it should satisfy the bigram model. Here is an example that uses the example model from exercise 4.3:

Example run:

   Enter a text and finish with an empty line:
   This is my test text.
   
   How many characters should the output text contain: 20
   This is the output text: tes my isthist t ist

[Hints]

Exercise 4.5*

In the game of hangman, a player attempts to find a word by guessing which characters are in the word. If the player manages to guess the word before making a certain number of failed guesses, he wins the game. Write a program that implements this game and allows the user to guess a word. The player should be allowed to make a maximum of seven failed guesses. Example run:

   The word is: _ _ _ _ _ _ _
   Please enter a character (7 attempts left): e
   Sorry, the word does not contain e
   The word is: _ _ _ _ _ _ _
   Please enter a character (6 attempts left): a
   Correct!
   The word is: _ a _ _ _ a _
   Please enter a character (6 attempts left): s
   Sorry, the word does not contain s
   The word is: _ a _ _ _ a _
   Please enter a character (5 attempts left): n
   Correct!
   The word is: _ a n _ _ a n
   Please enter a character (5 attempts left): g
   Correct!
   The word is: _ a n g _ a n
   Please enter a character (5 attempts left): m
   Correct!
   The word is: _ a n g m a n
   Please enter a character (5 attempts left): h
   You have found the word: hangman. Congratulations!

[Hints]


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