Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Bingo Challenge

by Matts (Deacon)
on May 19, 2002 at 16:29 UTC ( [id://167672]=note: print w/replies, xml ) Need Help??


in reply to Bingo Challenge

I feel so dumb. I thought this was a tough one to figure out. So anyway, I went ahead and wrote a script that generates nice PDF bingo cards:
#!/usr/bin/perl -w use strict; use PDFLib; my @words = <DATA>; chomp @words; warn("got @words\n"); my $cards = shift @ARGV || die "Must supply number of cards\n"; print "Making $cards bingo cards\n"; my $pdf = PDFLib->new( filename => "bingo.pdf", papersize => "a4", creator => "Matt Sergeant", author => "Heather Sergeant", title => "Bingo!", ); foreach my $i (1..$cards) { my %seen; print "Card $i\n"; $pdf->start_page; $pdf->set_font(face => "Helvetica", size => 30, bold => 1); $pdf->print_boxed("Bingo!", mode => "center", x => 0, y => 740, w +=> 595, h => 50); $pdf->rect(x => 100, y => 200, w => 400, h => 400); $pdf->stroke; $pdf->set_font(face => "Helvetica", size => 20, bold => 0); for my $x (1..4) { for my $y (1..4) { my $word; while (1) { my $index = rand(@words); $word = $words[$index]; if (!$seen{$word}) { $seen{$word}++; last; } } $pdf->print_boxed($word, mode => "center", x => (100 + (($ +x - 1) * 100)), y => (160 + (($y - 1) * 100)), w => 100, h => 100); if ($y != 1) { $pdf->move_to( 100, (200 + (($y - 1) * 100)) ); $pdf->line_to( 500, (200 + (($y - 1) * 100)) ); $pdf->stroke; } } if ($x != 1) { $pdf->move_to( (100 + (($x - 1) * 100)), 200 ); $pdf->line_to( (100 + (($x - 1) * 100)), 600 ); $pdf->stroke; } } } $pdf->finish; 1; __DATA__ WORDS HERE
Fill the DATA section with the things you want on the bingo card (maybe it's just numbers, in my case it was words). Remember to include at least 16 or you end up in an infinite loop ;-)

Replies are listed 'Best First'.
Re: Re: Bingo Challenge
by CharlesClarkson (Curate) on May 20, 2002 at 02:56 UTC

    You could speed things by shuffling @words right before the two inner loops.

    foreach my $i (1..$cards) { my %seen; . . . for my $x (1..4) { for my $y (1..4) { my $word; while (1) { my $index = rand(@words); $word = $words[$index]; if (!$seen{$word}) { $seen{$word}++; last; } } . . .
    becomes:
    use Algorithm::Numerical::Shuffle qw /shuffle/; . . . foreach my $i (1..$cards) { . . . @words = shuffle @words; for my $x (1..4) { for my $y (1..4) { my $word = shift @words; . . .

    HTH,
    Charles K. Clarkson
    Clarkson Energy Homes, Inc.
      Have you benchmarked that to see if it's faster?

      My suspicion would be that for large data sets it would be, but for smaller ones my naive rand() implementation would be quickest.

        Have you benchmarked that to see if it's faster?

        No, I used logic. (which is why I said it could be faster.) A shuffling algorithm takes the same time to shuffle on each pass. It's really just randomizing the array indexes. The idiom you were using varies each time through. The less words left, the longer it will take to produce an unchosen word. My thought was, perhaps you were unaware of shuffling or its application here.


        HTH,
        Charles K. Clarkson
        Clarkson Energy Homes, Inc.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-19 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found