Generating Combinations


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generating Combinations
# 1  
Old 03-19-2009
Generating Combinations

Hi,
I need to generate all combinations upto n-1 level,
if the input file looks like say,
A
B
C
D
.
.
....

I need to generate all combinations such that first value remains constant and the remaning are combined with all possible ways.

Output

A
AB
AC
AD
ABC
ABD
ACD
ABCD
........

The input file can contain maximum of 40 values. so the combinations could be extremely high.

Any suggestions atleast how to approach generating the combinations would be helpful. Thanks in advance!!!
# 2  
Old 03-19-2009
This's called permutations.
Here's a starting point - you can expand on this to read from a file:

nawk -v str='1 2 3 4 5 6 7 8 9' -f perm.awk

perm.awk:
Code:
BEGIN {
  str = ( str != "") ? str : "1 2 3"
  strN=split(str, arr, " ")
  permute(arr, 1, strN)
}

function printV(v, size,   i)
{
    for (i = 1; i <= size; i++) {
      printf("%4d", v[i] );
    }
    printf("\n");
}


function permute(v, start, n,    i,tmp)
{
  if (start == n) {
    printV(v, n);
  }
  else {
    for (i = start; i <= n; i++) {
      tmp = v[i];

      v[i] = v[start];
      v[start] = tmp;
      permute(v, start+1, n);
      v[start] = v[i];
      v[i] = tmp;
    }
  }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

All possible combinations problem

Post #2 is the original post. This is the first answer to post #2 Hi, try: awk ' { match($0,/1+/) b=substr($0,1,RSTART-1) e=substr($0,RSTART+RLENGTH,length) for(i=2^RLENGTH-2; i>0; i--) { s=x; d=i while(d) { s=(d%2==0?0:1) s ... (12 Replies)
Discussion started by: Scrutinizer
12 Replies

2. UNIX for Dummies Questions & Answers

Generating all possible combinations of values in field 1 (awk)

Input: A|1 B|2 C|3 D|4 Output: A+B|3 A+C|4 A+D|5 B+C|5 B+D|6 C+D|7 A+B+C|6 A+B+D|7 A+C+D|8 B+C+D|9 A+B+C+D|10 I only managed to get the output for pairs of $1 values (i.e. combination of length 2): (4 Replies)
Discussion started by: beca123456
4 Replies

3. Shell Programming and Scripting

All possible combinations

Hi, I have an input file like this a b c d I want to print all possible combinations between these records in the following way aVSb aVSc aVSd bVSc bVSd cVSd VS indicates versus. All thoughts are appreciated. (5 Replies)
Discussion started by: jacobs.smith
5 Replies

4. Programming

Words combinations without repetition

How can I get all combinations of 5 words from 10 words. For example I have 3 words and I want to get all combinations of 2 words. "A", "B", "C" it would like AB, BC, AC. Maybe you know some usefull code or example. Thanx a lot. P.S. Sorry if I'm not right enough cause I don't know English... (2 Replies)
Discussion started by: romeo5577
2 Replies

5. Shell Programming and Scripting

Combinations / Permutations

Hello Scrutinizer / Group , The shell script of awk that Scrutinizer made calculate all possible permutations in this case 3125 (5 numbers) but i want to have only the 126 possible combination. For now it does not matter the specific order of the combination numbers. I would appreciate it you... (1 Reply)
Discussion started by: csierra
1 Replies

6. Linux

Help with color combinations

Hi Team when i do, echo on my host box it returns (see below) # echo $PS1 \$ I need to set a color comination of my own for \u means for user : red for \h means for hostname: blue for \W means present working directory: pink for $ means for wht prompt : yellow Do i need to... (1 Reply)
Discussion started by: whizkidash
1 Replies

7. Programming

series of combinations

HI I have a series(sorted), which i require to create combinations. I am not getting the good code for doing this. My series should generate the following combinations... Please help me in getting this in C++. Thanks for your help. A: A A B: A B A B A B C: A ... (1 Reply)
Discussion started by: rameshmelam
1 Replies

8. Shell Programming and Scripting

Printing all combinations : Awk

$ cat key.txt #ID1 Start 1|AA1 2|AA2 3|AA3 4|AA4 #ID1 Complete #ID2 Start 1|BB1 2|BB2 3|BB3 #ID2 Complete I was required this output: AA1|BB1 AA1|BB2 AA1|BB3 (7 Replies)
Discussion started by: jkl_jkl
7 Replies

9. UNIX for Advanced & Expert Users

All Shortcut key combinations

Hi, I am using the Korn-Shell (ksh) and would like to know all the shortcut keys. For example: Shift + Insert etc. Thank you very much. Take care (0 Replies)
Discussion started by: --crimson--
0 Replies

10. UNIX for Advanced & Expert Users

Combinations

Hello All, i have two files, one of the format A 123 B 124 C 234 D 345 And the other A 678 B 789 C 689 D 567 I would like to combine them into one file with three columns: A 123 678 B 124 789 C 234 689 (4 Replies)
Discussion started by: Khoomfire
4 Replies
Login or Register to Ask a Question