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

Re: unordered sets of N elements

by hv (Prior)
on Jul 13, 2004 at 20:55 UTC ( [id://374137]=note: print w/replies, xml ) Need Help??


in reply to unordered sets of N elements

If you want an iterator (rather than a recursive function that returns all relevant combinations), that is easy too: it's just like counting in base n, except that when you overflow you reset the overflowing digit not to zero but to the same (new) value as the preceding digit:

sub next_iter { my($count, $die, $current) = @_; return '1' x $count unless $current; $current =~ s{ (?!$die) ((.) $die*) $ }{ my $d = $2 + 1; $d x length $2 }ex or return undef; return $current; } ... my $cur; print $cur while $cur = next_iter(3, 6, $cur);

Presenting the strings reversed instead allows a slightly cleaner solution with a (rare) useful use of 'cut' - just change the pattern to:

( (?>$die*) (.))
.. and the cut takes up the role of the negative lookahead in ensuring that our incremented digit cannot itself be a match for $die.

Hugo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-23 22:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found