Skip to content Skip to sidebar Skip to footer

Multidimensional Array Probability

Good day, I have been playing with this problem for some time now, and can't seem to analyze how to output the array probabilities, using PHP or javascript, had anyone manage to so

Solution 1:

This looks like the kind of challenge most textbooks give students to learn about recursive functions. Something like this would provide the desired output, and will work no matter how many arrays of values are in $arrayNms (as long as there is at least one):

functionprint_values($array, $index = 0, $base = "") {
    // check if there's another array of values after this one$is_last = !isset($array[$index + 1]);

    // loop through all values in the given sub-arrayforeach ($array[$index] as$value) {
        if ($is_last) {
            // if this is the last array of values, output the current valueecho$base . $value . PHP_EOL;
        } else {
            // otherwise, append this value to the base and process the next array
            print_values($array, $index + 1, $base . $value . ", ");
        }
    }
}

$arrayNms = [
    ["A", "B", "C"],
    ["Q", "P"],
    ["CC", "C3"]
];

print_values($arrayNms);

Output:

A, Q, CC
A, Q, C3
A, P, CC
A, P, C3
B, Q, CC
B, Q, C3
B, P, CC
B, P, C3
C, Q, CC
C, Q, C3
C, P, CC
C, P, C3

Solution 2:

In Java

publicclassMyClass{
    publicstaticvoid main(String args[]) {
        String[][] arr = {{"A","B","C"},{"P","Q"},{"CC","C3"}};
        print(arr, 0);
    }


    publicstaticvoidprint(String[][] arr, int depth)
    {
        if(depth == arr.length)
        {
            for(int i =0; i < arr.length - 1 ; i++)
            {
                System.out.print(arr[i][0] + ",");
            }
            System.out.print(arr[arr.length - 1][0]);
            System.out.println("\r");
        }
        else
        {
            for(int j =0; j < arr[depth].length ; j++)
            {
                String temp = arr[depth][j];
                arr[depth][j] = arr[depth][0];
                arr[depth][0] = temp;
                print(arr, depth + 1);
            }
        }
    }
}

Solution 3:

In JavaScript:

var myArr = [
    ["A", "B", "C"],
    ["Q", "P"],
    ["CC", "C3"]
];

for (var el1 in myArr[0]) {
    for (var el2 in myArr[1]) {
        for (var el3 in myArr[2]) {
            console.log(myArr[0][el1]+', '+myArr[1][el2]+', '+myArr[2][el3]);
        }    
    }  
}




/*
A, Q, CC
A, Q, C3
A, P, CC
A, P, C3
B, Q, CC
B, Q, C3
B, P, CC
B, P, C3
C, Q, CC
C, Q, C3
C, P, CC
C, P, C3
*/

var myArr = [
    ["A", "B", "C"],
    ["Q", "P"],
    ["CC", "C3"]
];

for (var el1 in myArr[0]) {
    for (var el2 in myArr[1]) {
        for (var el3 in myArr[2]) {
            console.log(myArr[0][el1]+', '+myArr[1][el2]+', '+myArr[2][el3]);
        }    
    }  
}

Solution 4:

Similar to Slawomir's JS answer:

<?php$items = [
    ["A", "B", "C"],
    ["Q", "P"],
    ["CC", "C3"]
];

foreach($items[0] as$i)
    foreach($items[1] as$j)
        foreach($items[2] as$k)
            printf("%s, %s, %s\n", $i, $j, $k);

Output:

A, Q, CC
A, Q, C3
A, P, CC
A, P, C3
B, Q, CC
B, Q, C3
B, P, CC
B, P, C3
C, Q, CC
C, Q, C3
C, P, CC
C, P, C3

Post a Comment for "Multidimensional Array Probability"