Permutations of an array of arrays of strings

前端 未结 5 850
鱼传尺愫
鱼传尺愫 2021-02-10 02:07

I simply cannot wrap my head around how to solve this problem and after a thorough search on Google with no results, I turn to you with hopes of a solution.

Given the sa

5条回答
  •  别那么骄傲
    2021-02-10 02:17

    Just for those wanting a PHP translation:

    function factor_permutations($lists) {
    
        $permutations = array();
        $iter = 0;
    
        while (true) {
    
            $num = $iter++;
            $pick = array();
    
            foreach ($lists as $l) {
                $r = $num % count($l);
                $num = ($num - $r) / count($l);
                $pick[] = $l[$r];
            }
    
            if ($num > 0) break;
            $permutations[] = $pick;
        }
    
        return $permutations;
    }
    
    print_r(factor_permutations(array(array('a', 'b'), array('1', '2', '3'), array('foo', 'bar'))));
    

提交回复
热议问题