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

Re: [FWP] Deck of cards [SPOILER]



1999-08-01-23:08:29 Tim Allwine:
> You are given N number of cards.
> You are to order the cards in such a way that when you
> deal the cards according to the RULES all of the cards
> on the table are in sequel order.
> 
> The RULES:
> 	Take the top card and place it face up on the table.
> 	Take the next card from the top of the deck and place it
> 	on the bottom of the deck.
> 	Deal the next card face up and place it on the table next to the first.
> 	Take the next card from the top of the deck and place it
> 	on the bottom of the deck.
> 	Repeat this until there are no more cards to deal.

This _was_ fun!

First, naturally, I wrote a dingus to do such a deal:

	while (@deck) {
	        print shift(@deck);
	        last unless @deck;
	        push @deck, shift @deck;
	        print ' ';
	}
	print "\n";

Hmm. Not instantly obvious how to stack the deck for that deal, but wait, hmm,
when I give it a simple run of sequential integers, it's printing where the
cards come from ... hmm ... Eureka!

	my(@t1) = 0 .. $#deck;
	my(@t2);
	while (@t1) {
	        push @t2, shift(@t1);
	        last unless @t1;
	        push @t1, shift @t1;
	}
	@deck[@t2] = @deck;

I.e. perform the shuffle on an array of the subscripts, then use an array
slice as the lvalue to pull the cards into stacked order.

-Bennett

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