Pseudocode or C #, which returns all possible combinations of sets for a number of variables

I have 3 variables with some possible values. For instance:

Var1 - possible values: 1,2,3
Var2 - possible values: a, b, c 
var3 - possible values: false, true

Can you help with an approach that returns all possible combinations?

The result will look like this:

   1,a,false
   1,a,true,
   1,b,false
   1,b,true,
   1,c,false
   1,c,true
   2,a,false
   2,a,true
   2,b,false
   Etc..

I would like the algorithm to be applied to any combination levels, for example, to an algorithm for working with 4 or 5 options with other possible values.

+4
source share
6 answers

It looks like you are trying to list Cartesian products . Assuming your objects are in list_of_lists, this recursive function in pseudo code will do this:

enumerate_cartesian_prducts(list_of_lists):

    if list_of_lists is empty:
        return [[]]

    this_list = list_of_lists[0]
    other_lists = list_of_lists[1: ]
    other_cartesian_products = []
    return [(e + other_cartesian_product) \
        for e in this_list and other_cartesian_product in other_cartesian_products]

, , , : , .

+1

n :

for each possible value v1 in var1
    for each possible value v2 in var2
        for each possible value v3 in var3
            print(v1,v2,v3);
        end for v3
    end for v2
end for v1

, , n ( var), . all_combinations.

list_of_lists=[[1...][a...][false...]];
current_comb=[];

all_combinations(list_of_lists,current_comb);

function all_combinations(list_of_lists,current_comb)
    if (list_of_lists=[])
        print(current_comb);
        return;
    end if
    current_list=list_of_lists[0];
    remaining_lists=list_of_lists[1:end];
    for each v in current_list
        tmp=current_comb;tmp.Append(v);
        all_combinations(remaining_lists,tmp);
    end for v

, .

+1

:

mix (A, B), . .

:

result = null
result = mix( result, one of your lists );
result = mix( result, another of your lists );
result = mix( result, yet another of your lists );
result = mix( result, yet another list );
result = mix( result, one more list );

(A, B)...

mix(A,B)
result = null
for each A
  for each B
    result += AB
return result
+1

, , is. :   set1 = [1, 2, 3]   set2 = [a, b, c]   set3 = [F, T]

- for. , 3 . : [[1, a, F], [1, a, T], [1, b, F],......] , (, Python) "append", 2 . :

myList = []  #empty list
for i in set1:
  for j in set2:
    for k in set3:
      myList.append([i, j, k])  #Appends 3-element list to big list

append, i, j k , . , .

0

- JavaScript, . ( #, , .)

var sets = [[1,2,3],["a","b","c"],[false,true]],
    result = [];

function f(arr,i){
  if (i == sets.length){
    result.push(arr);
    return;
  }
  for (var j=0; j<sets[i].length; j++){
    _arr = arr.slice(); // make a copy of arr
    _arr.push(sets[i][j]);
    f(_arr,i+1);
  }
}

f([],0)

:

console.log(result);

[[1,"a",false]
,[1,"a",true]
,[1,"b",false]
,[1,"b",true]
,[1,"c",false]
,[1,"c",true]
,[2,"a",false]
,[2,"a",true]
,[2,"b",false]
,[2,"b",true]
,[2,"c",false]
,[2,"c",true]
,[3,"a",false]
,[3,"a",true]
,[3,"b",false]
,[3,"b",true]
,[3,"c",false]
,[3,"c",true]]
0

, . , , , .

, , :

0 0 0 0 
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0 
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1

, , - 2 , 4 , 8 ​​.

, , :

Var1 - possible values: 0,1
Var2 - possible values: 0,1
Var3 - possible values: 0,1
Var4 - possible values: 0,1

, " ", , , " ", 1. , , "" , " ". .

Other answers focused on “give codz,” which really just rewards you for posting your home question here ... so I thought I'd explain a little.

-1
source

All Articles