Excellent!

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.

Regards
Neil


In reply to Re^4: Algorithm to fit photos into spaces on pages by SmugX
in thread Algorithm to fit photos into spaces on pages by SmugX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.