Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple foreach with over 37 million possibilities

Tags:

arrays

php

I've been tasked with creating a list of all possibilities using data in 8 blocks.

The 8 blocks have the following number of possibilities:

*Block 1: 12 possibilities
*Block 2: 8 possibilities
*Block 3: 8 possibilities
*Block 4: 11 possibilities
*Block 5: 16 possibilities
*Block 6: 11 possibilities
*Block 7: 5 possibilities
*Block 8: 5 possibilities

This gives a potential number of 37,171,200 possibilities.

I tried simply doing and limiting only to displaying the values returned with the correct string length like so:

foreach($block1 AS $b1){
    foreach($block2 AS $b2){
        foreach($block3 AS $b3){
            foreach($block4 AS $b4){
                foreach($block5 AS $b5){
                    foreach($block6 AS $b6){
                        foreach($block7 AS $b7){
                            foreach($block8 AS $b8){
                                if (strlen($b1.$b2.$b3.$b4.$b5.$b6.$b7.$b8) == 16)
                                {
                                    echo $b1.$b2.$b3.$b4.$b5.$b6.$b7.$b8.'<br/>';
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

However the execution time was far too long to compute. I was wondering if anyone knew of a simpler way of doing this?

like image 537
Numan1617 Avatar asked Feb 16 '12 11:02

Numan1617


People also ask

Can I use foreach() multiple times in a collection?

foreach is used to enumerate individual items in a collection. So no you can't. You have to use it one after the other. It would be better to use: void myfunc () {} foreach (var item1 in collection1) {myfunc ();} foreach (var item2 in collection2) {myfunc ();} foreach (var item1 in collection1) foreach (var item2 in collection2) { myfunc (); }

Can you use multiple variables in a foreach loop?

No, you cannot use multiple variables in a foreach, in loop. Check the language reference. What would happen if each collection had a different number of items? If you want to iterate over both collections, try using a union: foreach (var item1 in collection1.Union (collection2)) { ...

What is an example of foreach in PowerShell?

ForEach PowerShell Examples. Example 1: Creating a File in Each Sub-Folder in a Directory using the ForEach Statement. Example 2: Reading the Contents of each Text File in Sub-Directories. Example 3: Getting Services and Starting Them using the ForEach-Object CmdLet.

What is the difference between foreach and ForEach-Object?

If foreach is a statement and can only be used in a single way, ForEach-Object is a cmdlet with parameters that can be employed in a lot of different ways. Like the foreach statement, the ForEach-Object cmdlet can iterate over a set of objects.


1 Answers

You could improve your algorithm by caching the string prefixes and remember their lengths. Then you don’t have to do that for each combination.

$len = 16:

// array for remaining characters per level
$r = array($len);
// array of level parts
$p = array();
foreach ($block1 AS &$b1) {
    // skip if already too long
    if (($r[0] - strlen($b1)) <= 0) continue;
    $r[1] = $r[0] - strlen($b1);
    foreach ($block2 AS &$b2) {
        if (($r[1] - strlen($b2)) <= 0) continue;
        $r[2] = $r[1] - strlen($b2);
        foreach ($block3 AS $b3) {
            // …
            foreach ($block8 AS &$b8) {
                $r[8] = $r[7] - strlen($b8);
                if ($r[8] == 0) {
                    echo implode('', $p).'<br/>';
                }
            }
        }
    }
}

Additionally, using references in foreach will stop PHP using a copy of the array internally.

like image 72
Gumbo Avatar answered Sep 29 '22 23:09

Gumbo