0

I have a two dimensional array of strings values:

ABC
-D-
-FG

where "-" means empty value. What I want to achieve is to get all combinations of array elements "from left to right", so the results of given array should be:

ABC
ADC
AFC
AFG
ABG
ADG

Empty values should be ommitted and order of items should be kept. The problem is the number of array columns and rows is not fixed. I tried to play with loop in loop (and loop) and recursion but with no success. Also cartesian product is usless in this case as it will produce needless combinations (I tried to create sql temporary tables dynamically and store the values in them making cross join). Coud someone to show me a way, please?

5
  • 1
    Where is ABCG etc.? It looks like the G is in a 4th position. (I replaced your screenshot with text, but refer to it i.sstatic.net/LNClv.png and please correct me if I'm wrong...) Commented Aug 27, 2018 at 20:04
  • ABCG is not required. We only build as many charakter as array columns count. From left to right. If array had 4 columns 4th character would be required. Commented Aug 28, 2018 at 5:26
  • But there are 4 columns in the 3rd row. How would the combinations differ if the 3rd row were "-FG"? Commented Aug 28, 2018 at 5:32
  • 1
    You are correct, there should be 3 columns in each row. Third row should be -FG.That's my mistake, sorry. Commented Aug 28, 2018 at 6:03
  • Ok, edited for you Commented Aug 28, 2018 at 6:10

1 Answer 1

1

If I understand your requirements correctly, you can do this by using an array to hold, for each column, the index of the row containing the value to be output. Initially this array will hold the first non-empty row for each column. At each iteration you increment, starting at the lowest column, the row to be indexed, until you get a non-empty value or you reach the end of the rows, in which case you set the row back to the first non-empty value and move on to the next column. This resembles the action of an old odometer in a car.

Here's some Java code to illustrate (sorry, I'm not familiar with VB.Net)

 char[][] values = { {'A', 'B', 'C'}, {'-', 'D', '-'}, {'-', 'F', 'G'} }; int rows = values.length; int cols = values[0].length; int[] idx = new int[cols]; // for each column, find the first non-empty character int i=0; for(; i<cols; i++) { while(idx[i] < rows && values[idx[i]][i] == '-') idx[i]++; if(idx[i] == rows) break; } // if a column was missing a non-empty value then we can't proceed if(i<cols) { System.out.println("Missing value in column " + i); return; } while(true) { // print current solution for(int j=0; j<cols; j++) System.out.print(values[idx[j]][j]); System.out.println(); int k=0; for(; k<cols; k++) { // find next non-empty character do idx[k]++; while(idx[k] < rows && values[idx[k]][k] == '-'); // if there was one, break if(idx[k] < rows) break; // else, wrap around to 0, but then find next non-empty row idx[k] = 0; while(values[idx[k]][k] == '-') idx[k]++; } // if the last index wrapped around then we're done if(k == cols) break; } 

Output:

ABC ADC AFC ABG ADG AFG 
Sign up to request clarification or add additional context in comments.

1 Comment

Possible enhancement for large data set: squeeze columns before main treatment to make {{'A', 'B', 'C'}, {'-', 'D', 'G'}, {'-', 'F', '-'}}; with column counts {1,3,2} to avoid instant checking inside main loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.