Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
XP is just a number
 
PerlMonks  

Re: (Golf) The Animal Game

by dragonchild (Archbishop)
on Jan 23, 2002 at 02:18 UTC ( #140747=note: print w/ replies, xml ) Need Help??


in reply to (Golf) The Animal Game

I'll come in at 153 characters. I'm sure it has to be better than that ... I'm using too many temp vars.

Updated: Forgot to change my datastructure from a hash of hashes to a hash of arrays. That caused the bug jarich noticed. ++!

#!/usr/local/bin/perl my %tree = ( 'larger than a breadbox' => [ 'cat', 'giraffe', ], ); sub a { print 'Is your animal ', shift, "?\n"; my $answer = <STDIN>; chomp $answer; return 1 if $answer =~ /y/i; return 0; } sub t { print "What is your animal?\n"; my $answer = <STDIN>; chomp $answer; return $answer; } sub i { my ($g, $u) = @_; print "What is a question that is true for $u, but false for $g?\n +"; my $answer = <STDIN>; chomp $answer; $answer =~ s/Is your animal //; return $answer; } sub w { print "Do you want to continue?\n"; my $answer = <STDIN>; chomp $answer; return 1 if $answer =~ /y/i; return 0; } #234567890#234567890#234567890#234567890#234567890#234567890#234567890 +#234567890 sub f { ($s,%t)=@_;$q=$s;do{{$v=a($q);$a=$t{$q}[$v];$q=$a,redo if$t{$a};$q=$s, +next if a( $a);$n=&t;$o=i($a,$n);$t{$o}=[$t{$q}[$v],$n];$t{$q}[$v]=$o;$q=$s}}whil +e&w } f( 'larger than a breadbox',%tree); __END__

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.


Comment on Re: (Golf) The Animal Game
Download Code
Re: Re: (Golf) The Animal Game
by jarich (Curate) on Jan 23, 2002 at 06:35 UTC
    I can't golf it better, but I can fix a bug I've spotted. If you say your animal isn't bigger than a bread box, it guesses your animal to be "1", which isn't much fun. These are my changes:
    my %tree = ( 'Is your animal larger than a breadbox' => { '1' => 'giraffe', '0' => 'cat', }, ); # anon hashref rather than array ref #234567890#234567890#234567890#234567890#234567890 sub f{ ($s,%t)=@_;$q=$s;do{{$v=a($q);$a=$t{$q}->{$v};$q= $a,redo if$t{$a};$q=$s,next if a($a);$n=&t;$o=i($a ,$n);$t{$o}={0=>$t{$q}->{$v},1=>$n};$t{$q}->{$v}= $o;$q=$s;}}while&w }
    Sorry about the extra 13 characters. ;) It's a cool program though, I've got to admit that the idea had me stumped 'til I saw yours and Tilly's answers.

    jarich

    update: I can't count (or rather I forgot the indexing), 13 extra characters, not 6.

      It's supposed to be anon listref. I had it as anon hashref, then switched it to anon listref. Updated my original node.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Re: (Golf) The Animal Game
by dragonchild (Archbishop) on Jan 23, 2002 at 22:03 UTC
    Heh. I found an interesting bug in mine. If you use the same question for two branches, it overwrites. This is due to the 1-D flattening of the N-D structure. *grins* I don't feel like fixing it. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://140747]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2014-04-20 20:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (487 votes), past polls