Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
i'm with redsquirrel; it's my second golf outing. i'm in with 183. obviously i'm not on the pro tour.

here's the full code:

#!/usr/local/bin/perl -w use strict; $|=1; # www.PerlMonks.org 140734|(Golf) The Animal Game # ask the question passed, return true or false sub a($) { my ($qu, $ans) = (shift); while(!$ans) { print "\n$qu?\n"; chomp ($ans = <STDIN>) } $ans =~ m/y/io ? 1 : 0; }; # ask if animal is passed value, return true or false sub g($) { my ($ani, $ans) = (shift); while(!$ans) { print "\nis animal $ani?\n"; chomp ($ans = <STDIN>) + } $ans =~ m/^y/io ? 1 : 0; }; # ask what animal, return user input sub t() { my $ans; while(!$ans) { print "\nwhat animal?\n"; chomp ($ans = <STDIN>) } $ans; }; # get question to identify animal: # true for new, false for old; return question sub i($$) { my ($old, $new, $qu) = (shift, shift); while(!$qu) { print "\nenter a question false for $old but true for $new:\n" +; chomp ($qu = <STDIN>); } $qu; }; # ask to continue, return true or false sub w() { a('play') ? 1 : exit 0 }; # question/answer data structure: hash of arrays my %q = ( "larger than a breadbox" => ['cat', 'giraffe'], ); no strict; while(w){($k,$v)=each%q;$k||redo;$u=$$v[a($k)];next if(g($u));while(($ +l,$x)=each%q){next unless$$x[0] eq $u;a($l)?$y=$$x[1]:goto N;goto E i +f(g($y))}N:$b=i($u,$c=t);$q{$b}=[$u,$c];E:}
i have a commented version, with slightly better variable names...

no strict; { while(w){ # select a question ($k,$v)=each%q; # try again if no more questions (end of hash) $k||redo; # ask question, get proper animal from question hash $t2=$$v[a($k)]; # guess animal, end loop if found answer next if(g($t2)); # lookup keys with value[0] matching $t2 while(($k2,$v2)=each%q){ next unless$$v2[0] eq $t2; # if key exists, ask question # if answer negative, ask new animal a($k2)?$t5=$$v2[1]:goto NEW; goto END if(g($t5)); } # else, ask new animal NEW: # ask new animal $t4=i($t2,$t3=t); # add to question hash $q{$t4}=[$t2,$t3]; END: } }
thanks for the fun.

~Particle


In reply to Re: (Golf) The Animal Game by particle
in thread (Golf) The Animal Game by Masem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (8)
    As of 2015-07-29 23:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (269 votes), past polls