开发者

How to get ALL combinations of a list of words using ANY number of words

I have searched but I can't find anything that matches my query. I have seen lots of solutions where people want all combinations of numbers/words that use ALL the options, but none like this开发者_JS百科...

Here's an example:

apple pear

This should generate:

apple

pear

apple pear

pear apple

Or even...

apple pear banana

apple

pear

banana

apple pear

apple banana

pear banana

...

...

banana pear apple

The key is, ALL possible combinations that use any of the words zero or one times in ANY order. :)


FINAL ANSWER AT THE BOTTOM


Pseudocode (has not been tested)

$str = "apple pear banana";
$str_splode = explode(' ',$str);

echo showCombo($str_splode[0], $str_splode);

function showCombo($str, $arr){
    $ret = '';
    foreach($arr as $val){
       if($val != $str)
           $ret .= $str.showCombo($val, $arr);
    }
    return $ret;
}

Running code: http://codepad.org/IUPJbhI7

<?php

$str = "apple pear banana orange";
$str_splode = explode(' ',$str);
print_r(showCombo(array(), $str_splode));

function showCombo($str_arr, $arr){
    $ret = array();
    foreach($arr as $val){
       if(!in_array($val, $str_arr)){
           $temp = $str_arr;
           $temp[] = $val;
           print_r($temp);
           $comb = showCombo($temp, $arr);
           if(count($comb) > 0)
              $ret[] = $comb;
       }
    }
    return $ret;
}
?>

This returns all possible combinations


Or this one looks better: http://codepad.org/KCLeRUYs

<?php

$str = "apple pear banana orange";
$str_splode = explode(' ',$str);
print_r(showCombo(array(), $str_splode));

function showCombo($str_arr, $arr){
    $ret = array();
    foreach($arr as $val){
       if(!in_array($val, $str_arr)){
           $temp = $str_arr;
           $temp[] = $val;
           $ret[$val] = $temp;
           $ret[$val][] = showCombo($temp, $arr);
       }
    }
    return $ret;
}

?>

Or if you want to look at flat keys: http://codepad.org/95aNQzXB


Final Answer:

And this one lists them all: http://codepad.org/vndOI9Yj

<?php

$str = "apple pear banana orange";
$str_splode = explode(' ',$str);
$combos = showCombo(array(), $str_splode);
foreach($combos as $key=>$array){
    echo $key.PHP_EOL;
    displayArrayByKey($key, $array);
}


function displayArrayByKey($str, $arr){
    foreach($arr as $key=>$array){
          $string = $str. " " . $key;
          echo $string . PHP_EOL; 
          if(count($array)> 0){
              displayArrayByKey($string, $array);
          }
    }
}

function showCombo($str_arr, $arr){
    $ret = array();
    foreach($arr as $val){
       if(!in_array($val, $str_arr)){
           $temp = $str_arr;
           $temp[] = $val;
           $ret[$val] = showCombo($temp, $arr);
       }
    }
    return $ret;
}

?>


You can download this class: http://pear.php.net/package/Math_Combinatorics

and use it like:

$combinatorics = new Math_Combinatorics;

$words_arr = array(
    'one'   => 'a',
    'two'   => 'b',
    'three' => 'c',
    'four'  => 'd',
    );

for ($i=count($words_arr)-1;$i>=1;$i--) {
    echo '<br><br>' . $i . ':<br>';
    $combinations_arr = $combinatorics->combinations($words_arr, $i);
    foreach ($combinations_arr as $combinations_arr_item) {
        echo implode(', ', $combinations_arr_item) . '<br>';
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜