in reply to combinations of given string
I can't (yet) work out how to code _next() in Perl.
#! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => '_1059792', CLEAN_AFTER_BUILD =>0; unsigned int _next( unsigned int v ) { unsigned int t = (v | (v - 1)) + 1; unsigned int w = t | ((((t & -t) / (v & -v)) >> 1) - 1); return w; } END_C sub permIt { my $l = @_; my @chars = grep $_ ne '-', @_; my $n = my $s = ( 1 << @chars ) -1; do { my $bits = pack 'V', $n; my @copy = @chars; my $str = join '', map{ vec( $bits, $_, 1 ) ? shift( @copy ) : '-' } 0 .. $l-1; print $str; $n = _next( $n ); } until( $n > ( $s << ( $l - @chars ) ) ); } permIt( split '', 'ABC---' );; __END__ ABC--- AB-C-- A-BC-- -ABC-- AB--C- A-B-C- -AB-C- A--BC- -A-BC- --ABC- AB---C A-B--C -AB--C A--B-C -A-B-C --AB-C A---BC -A--BC --A-BC ---ABC
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: combinations of given string
by Athanasius (Archbishop) on Oct 26, 2013 at 07:28 UTC | |
by BrowserUk (Patriarch) on Oct 26, 2013 at 10:40 UTC | |
by Athanasius (Archbishop) on Oct 26, 2013 at 14:45 UTC | |
by BrowserUk (Patriarch) on Oct 26, 2013 at 17:19 UTC | |
by aitap (Curate) on Oct 26, 2013 at 17:29 UTC | |
| |
by LanX (Saint) on Oct 27, 2013 at 00:36 UTC | |
|
Re^2: combinations of given string
by Anonymous Monk on Oct 26, 2013 at 06:12 UTC | |
by BrowserUk (Patriarch) on Oct 26, 2013 at 06:23 UTC |