Hi.
At our shop we often like to produce generalized solutions. For the case of combinations and permutations, a perl
module:
Math::Combinatorics can produce:
use Math::Combinatorics;
my @n = qw(a b c);
print join("\n", map { join " ", @$_ } combine(2,@n)),"\n";
Output:
a b
a c
b c
For any set of strings that you get into array n
.
That allows easy parameterization of generating combinations. Of course, the infrastructure code takes up some room, so utilities can grow to a hundred lines. For many this would not be worth the effort, but consider how easy it is to produce something like the demonstration script:
#!/usr/bin/env bash
# @(#) s1 Demonstrate combinations from CL, file.
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C combination permutation my-columns dixf
FILE=${1-data1}
pl " Input data file $FILE:"
cat $FILE
pl " Results, read from command line, combinations of 2:"
combination -2 a b c d
pl " Results, read from command line, combinations of 3:"
combination -3 a b c d
pl " Results, read from file, columnized:"
combination -2 < $FILE |
my-columns
pl " Results, read from file, columnized, better separation:"
combination -2 < $FILE |
my-columns -s='/ / | /'
pl " Results, read from command line, but permutations, columnized:"
permutation a b c d |
my-columns -s='/ / | /'
pl " Details of combination:"
dixf combination
exit 0
producing:
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.7 (jessie)
bash GNU bash 4.3.30
combination (local) 1.6
permutation (local) 1.1
my-columns (local) 1.6
dixf (local) 1.42
-----
Input data file data1:
a b c d
E F G H
-----
Results, read from command line, combinations of 2:
b c
b d
b a
c d
c a
d a
-----
Results, read from command line, combinations of 3:
a c d
a c b
a d b
c d b
-----
Results, read from file, columnized:
a b a d a F a H b d b F b H c E c G d E d G E F E H F H
a c a E a G b c b E b G c d c F c H d F d H E G F G G H
-----
Results, read from file, columnized, better separation:
a b | a E | a H | b E | b H | c F | d E | d H | E H | G H
a c | a F | b c | b F | c d | c G | d F | E F | F G
a d | a G | b d | b G | c E | c H | d G | E G | F H
-----
Results, read from command line, but permutations, columnized:
a b c d | a c d b | b a c d | b c d a | c a b d | c b d a | d a b c | d b c a
a b d c | a d b c | b a d c | b d a c | c a d b | c d a b | d a c b | d c a b
a c b d | a d c b | b c a d | b d c a | c b a d | c d b a | d b a c | d c b a
-----
Details of combination:
combination Produce n combinations of arguments or file: [-n] a1 a2 ... am (what)
Path : ~/bin/combination
Version : 1.6
Length : 172 lines
Type : Perl script, ASCII text executable
Shebang : #!/usr/bin/env perl
Help : probably available with -h,--help
Modules : (for perl codes)
warnings 1.23
strict 1.08
English 1.09
Carp 1.3301
Data::Dumper 2.151_01
Getopt::Long 2.42
feature 1.36_01
Math::Combinatorics 0.09
We currently don't post our local codes, but we provide some details on what is done, and how it is accomplished.
Best wishes ... cheers, drl