Perl Quiz-of-the-Week #10

Mark Jason Dominus <[email protected]> Tue, 28 Jan 2003 15:24:41 -0500
Newsgroups gmane.comp.lang.perl.qotw.quiz-of-the-week
Organization Plover Systems
Message-ID <[email protected]>

IMPORTANT: Please do not post solutions, hints, or other spoilers
        until at least 60 hours after the date of this message.
        Thanks.

IMPORTANTE: Por favor, no enviéis soluciones, pistas, o cualquier otra
        cosa que pueda echar a perder la resolución del problema hasta
        que hayan pasado por lo menos 60 horas desde el envío de este
        mensaje. Gracias.

IMPORTANT: S'il vous plaît, attendez au minimum 60 heures après la
        date de ce message avant de poster solutions, indices ou autres
        révélations. Merci.

WICHTIG: Bitte schicken Sie keine Lösungen, Tipps oder Hinweise für
        diese Aufgabe vor Ablauf von 60 Stunden nach dem Datum dieser
        Mail. Danke.

BELANGRIJK: Stuur aub geen oplossingen, hints of andere tips in de
        eerste 60 uur na het verzendingstijdstip van dit
        bericht. Waarvoor dank.

VNIMANIE: Pozhalujsta ne shlite reshenija, nameki na reshenija, i
        voobshe lyubye podskazki v techenie po krajnej mere 60 chasov
        ot daty etogo soobshenija.  Spasibo.

Qing3 Zhu4Yi4: Qing3 Ning2 Deng3Dao4 Jie1Dao4 Ben3 Xin4Xi2 Zhi1Hou4 60
        Xiao3Shi2, Zai4 Fa1Biao3 Jie3Da2, Ti2Shi4, Huo4 Qi2Ta1 Hui4
        Xie4Lou4 Da2An4 De5 Jian4Yi4.  Xie4Xie4.

----------------------------------------------------------------

Here's an old chestnut of a computer entertainment: The
'guess-the-animal' program.  The human thinks of an animal.  The
computer tries to guess what the animal is by asking yes-or-no
questions.

The program maintains a decision tree of questions and answers, say
like this:


                Does it fly?
                     /\
                yes /  \ no
                   /    \
          Does it have   salmon
              feathers?
                 /\
            yes /  \ no
               /    \
           sparrow  fruit
                     bat

The program starts at the root and asks the question it finds there.
Depending on the user's response, it proceeds down the tree to the
'yes' or 'no' branch.  Leaf nodes in the tree represent animals; when
the program reaches one it asks (for example) "is it a sparrow?".  If
the answer is 'yes', the computer wins; if not, it prepares to extend
the tree.

To extend the tree, the computer asks a series of three questions:

        What animal were you thinking of?

                (Suppose the user replies "duck".)

        What is a question that would distinguish a duck from a sparrow?

                (Suppose the user replies "does it have webbed feet")

        For a duck, the answer would be?

                (The user replies "yes".)

The program then modifies its database to look like this:


                Does it fly?
                     /\
                yes /  \ no
                   /    \
          Does it have   salmon
              feathers?
                 /\
            yes /  \ no
               /    \
       Does it have fruit
       webbed feet?  bat
            /\
       yes /  \ no
          /    \
        duck   sparrow

Next time it plays the game, if the user says that their animal flies
and has feathers, the program will not ask right away if it was a
sparrow; instead, it will ask whether the animal has webbed feet, and
then if so it will ask if it is a duck, and if not it will ask if it
is a sparrow.

The program should record its database in a file so that it can
remember it from run to run.