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
    the number of different combinations is (c + w)! / c! w!

    Why does that formula seem familiar? (Did you derive it or find a reference to it?)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Why does that formula seem familiar?
      It looks like the number of w-combinations from a set of c+w elements: C(c+w,w) = (c+w)!/(c+w-w)!w!
        It looks like the number of w-combinations from a set of c+w elements: C(c+w,w) = (c+w)!/(c+w-w)!w!

        Yes, that's probably it.

        I mean, that is definitely the derivation of the formula; and it is probably where I recognise it from. Still, there is an itch in the back of my brain that is associating it with something quite different?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^5: combinations of given string
by LanX (Saint) on Oct 27, 2013 at 00:36 UTC