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

Re: combinations of given string

by LanX (Canon)
on Oct 26, 2013 at 23:54 UTC ( #1059865=note: print w/ replies, xml ) Need Help??


in reply to combinations of given string

generic solution for arbitrary number of ordered sets to be combined

use v5.10.0; use warnings; use strict; use Data::Dump qw/pp/; my @sets = ( ["A".."C"], [("-") x 3] ); my (@path,@results); sub branch { my $done=1; for my $set ( @sets ){ if (@$set ) { $done=0; push @path, shift @$set; branch(); unshift @$set, pop @path; } } if ($done){ push @results, join "",@path; } } branch(); pp \@results; __END__ [ "ABC---", "AB-C--", "AB--C-", "AB---C", "A-BC--", "A-B-C-", "A-B--C", "A--BC-", "A--B-C", "A---BC", "-ABC--", "-AB-C-", "-AB--C", "-A-BC-", "-A-B-C", "-A--BC", "--ABC-", "--AB-C", "--A-BC", "---ABC", ]

Cheers Rolf

( addicted to the Perl Programming Language)

update

simplified code

update

Formula: be s_1,s_2,...s_n the size of n sets in @sets, the number of combinations is then

fac( sum( s_i) ) ------------------- prod ( fac (s_i) )

or

( Σ si ) ! 
---------- 
  Π (si!)


Comment on Re: combinations of given string
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (18)
As of 2014-07-31 14:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (249 votes), past polls