in reply to Re^3: combinations of given string
in thread combinations of given string
If you're really bored, you might try to resolve the table at the end -- characters down; wildcards across -- to a formula.
For c characters and w wildcards, the number of different combinations is (c + w)! / c! w!:
#! perl use strict; use warnings; use constant { WILDCARDS => 9, CHARACTERS => 10, }; my @table = [ 0 .. WILDCARDS ]; for my $char (1 .. CHARACTERS) { push @{ $table[$char] }, $char; push @{ $table[$char] }, combinations($char, $_) for 1 .. WILDCARD +S; } my @widths = map { length } @{ $table[-1] }; print "\n"; printf '%*s|', $widths[$_], ($_ || '') for @{ $table[0] }; print "\n"; printf '%s+', ('-' x $_) for @widths; print "\n"; for my $row (1 .. CHARACTERS) { printf '%*d|', $widths[$_], $table[$row][$_] for 0 .. WILDCARDS; print "\n"; } sub combinations { my ($c, $w) = @_; return factorial($c + $w) / (factorial($c) * factorial($w)); } sub factorial { my ($n) = @_; my $f = 1; $f *= $_ for 2 .. $n; return $f; }
Output:
0:30 >perl 760_SoPW.pl | 1| 2| 3| 4| 5| 6| 7| 8| 9| --+--+--+---+----+----+----+-----+-----+-----+ 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 2| 3| 6| 10| 15| 21| 28| 36| 45| 55| 3| 4|10| 20| 35| 56| 84| 120| 165| 220| 4| 5|15| 35| 70| 126| 210| 330| 495| 715| 5| 6|21| 56| 126| 252| 462| 792| 1287| 2002| 6| 7|28| 84| 210| 462| 924| 1716| 3003| 5005| 7| 8|36|120| 330| 792|1716| 3432| 6435|11440| 8| 9|45|165| 495|1287|3003| 6435|12870|24310| 9|10|55|220| 715|2002|5005|11440|24310|48620| 10|11|66|286|1001|3003|8008|19448|43758|92378| 0:30 >
Conclusion: apparently I was really bored. :-)
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: combinations of given string
by BrowserUk (Patriarch) on Oct 26, 2013 at 17:19 UTC | |
by aitap (Curate) on Oct 26, 2013 at 17:29 UTC | |
by BrowserUk (Patriarch) on Oct 26, 2013 at 22:18 UTC | |
|
Re^5: combinations of given string
by LanX (Saint) on Oct 27, 2013 at 00:36 UTC |