Home > Software design >  I am facing struggle to get the result for nested object list to probability tree result form. As Fo
I am facing struggle to get the result for nested object list to probability tree result form. As Fo

Time:03-29

Can someone help me for a snip of code.

void main() {
  List<List<String>> testList = [["a","b","c"], ["1","2"], ["Y","Z"]];
  
 // Result list I want =>  a1Y, a1Z, a2Y, a2Z, b1Y, b1Z, b2Y, b2Z, c1Y, c1Z, c2Y, c2Z
  
}
  

CodePudding user response:

Similar question Generate all combinations from multiple lists

Answer Source: https://stackoverflow.com/a/17193002/6576315


Dart Version:

void generatePermutations(List<List<String>> lists, List<String> result, int depth, String current) {
    if (depth == lists.length) {
        result.add(current);
        return;
    }

    for (int i = 0; i < lists.elementAt(depth).length; i  ) {
        generatePermutations(lists, result, depth   1, current   lists.elementAt(depth).elementAt(i));
    }
}

Usage:

  List<List<String>> testList = [["a","b","c"], ["1","2"], ["Y","Z"]];
  
  List<String> result = <String>[];
      
  generatePermutations(testList, result, 0, "");
  
  print(result);

// prints: [a1Y, a1Z, a2Y, a2Z, b1Y, b1Z, b2Y, b2Z, c1Y, c1Z, c2Y, c2Z]

Do upvote the original source if it works

  • Related