[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Deck of cards



Bernie Cosell said...
>
>Seems like the easiest way to do this is just to run the process in 
>reverse:
>
>my $cards = $ARGV[0] ;
>
>@deck = ($cards);  # Ready to deal last card onto the table
>
>for (my $i = $cards-1; $i > 0; $i -= 1)
>{
>    # Move bottom card to the top, then pick up next card from table
>    unshift @deck, $i, pop @deck;
>}
>print join(", ", @deck), "\n" ;
>

Sweet. I never thought about in reverse.

Here is our solution.

my $num = $ARGV[0];
my @deck;
my $d;
my $f = 0;

@deck = map { undef } 1..$num; # prepare the deck

while($d != $num) {
    for my $card (0..$#deck) {
		if( $f && !defined $deck[$card] ) {
			$f = 0;
			next;
		}
		unless ( defined $deck[$card] ) {
			$d += 1;
			$deck[$card] = $d;
			$f = 1;
		}
    }
}

print ("@deck\n");

-- 
Tim Allwine
IX Development Laboratories

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe