Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: combinations of given string

by LanX (Bishop)
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!)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1059865]
help
Chatterbox?
[Corion]: LanX: Broken links in what sense?
[Discipulus]: neurocent goes always up with the same curve of population raise
[marto]: believe in derp coin
[Corion]: Oh - I see ...
[LanX]: like http://cb60. datenzoo.de/:// perlmonks.org/ index.pl?node_id= 1215217
[Discipulus]: username links i bet
[Corion]: (also, let me recommend https://mini-cb60. datenzoo.de/ over that, as it has https :)
[LanX]: all links
[Corion]: I guess I should make the certificate valid for both hostnames
[LanX]: and broken links ;p

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-05-25 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?