Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^4: Algorithm to fit photos into spaces on pages

by SmugX (Beadle)
on Nov 16, 2005 at 17:07 UTC ( #509114=note: print w/replies, xml ) Need Help??

in reply to Re^3: Algorithm to fit photos into spaces on pages
in thread Algorithm to fit photos into spaces on pages


I was just playing around with your first lot of code when you posted your second update! :-) This works really well, and I can definitely take this idea and run with it.

I noticed that it seemed to have a tendency to use the earlier layouts - most books seemed to often use layouts 1-6, but rarely the later layouts.

I assumed this was because the layouts were always being presented to the regex engine in the same order. Indeed, if I tweak your second example to randomise the order, it seems to use a more general spread of layouts, with the (interesting?) side effect of generating a different valid book each time.

Then I noticed that in your second example at least, the photos aren't necessarily all used. I assumed I can prepend a '^' and append a '$' to the regexp, and this seems to resolve the issue.

I'm hence left with:

use warnings; use strict; my $desired_page_count = 10; # Structure holding all the different page layouts. ('l' = Landscape, +'p' = Portrait) my $pages = { ll => '01', pp => '02', lp => '03', pl => '04', lpp => '05', pll => '06', plp => '07', lpl => '08', lll => '09', ppp => '10', ppl => '11', llp => '12', ppll => '13', llpp => '14', }; # Construct a hash of regexen: each value is all the patterns except f +or the # key. This way, they don't need to be constructed repeatedly during t +he matching my @patterns = keys %$pages; my %all_except = map {my $k=$_; ($k => join '|', sort {rand(1) <=> ran +d(1)} grep {$_ ne $k} @patterns)} @patterns; my $photos = "llppllpplpppllplpplpplpllplpll"; # Make a really long layout $photos .= substr 'lp', rand 2 for 1..50; $desired_page_count = int( length($photos)/ 2.7 ); my $page_regex = join '|', sort {rand(1) <=> rand(1)} @patterns; my $regex = "^($page_regex)"; $regex .= "((??{\$all_except{\$$_}}))" for 1..($desired_page_count - 1 +); $regex .= "\$"; print "\$regex is $regex\n"; use re 'eval'; if (my @pages = $photos =~ /$regex/) { print "$pages->{$_}\n" for @pages; } else { print "Could not come up with no-repeat layout in $desired_page_cou +nt pages\n"; }

Thanks for all your help, Roy. ++ your posts.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://509114]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2018-04-25 15:05 GMT
Find Nodes?
    Voting Booth?