"Jonathan E. Paton" <[EMAIL PROTECTED]> writes:

>> I was looking at the implementation of the FY shuffle that I see in 
>> Perlfaq4, namely:
>> 
>>      sub fy1 {
>>          my $deck = shift;  # $deck is a reference to an array
>>          my $i = @$deck;
>>          while (--$i) {
>>              my $j = int rand ($i+1);
>>              @$deck[$i,$j] = @$deck[$j,$i];
>>          }
>>      }
>> 
>> And I thought "guh, that looks a bit off" -- notably, it dies when given an 
>> empty array.
>
> Now I understand... that's why shuffle.pl failed the test suite.
> ;-) I tried to extract the algorithm from the FAQ textually, and
> then run it.  It worked for manual checking - but it is now obvious
> now why it was rejected.
>
> BTW: It wasn't intended to be a serious submission, dig it out from
> the list of submissions from that game (what was it called?).  If we
> weren't in the middle of a golf tournament, I'd suggest golfing the
> technique :)

Nothing wrong with golfing it. It's been a while since we had an
'everyone pitch in and shave those characters' golf session on this
list. Assuming we're still in a sub...

sub fy1 {*d=pop;$i=@d;@d[$j,$i]=@d[$i,$j=rand$i+1]while--$i}


-- 
Piers

   "It is a truth universally acknowledged that a language in
    possession of a rich syntax must be in need of a rewrite."
         -- Jane Austen?

Reply via email to