如何使用任意数量的单词获取单词列表的所有组合

发布于 2024-12-05 19:16:17 字数 432 浏览 2 评论 0原文

我已经搜索过,但找不到任何与我的查询匹配的内容。我见过很多解决方案,人们希望使用所有选项的所有数字/单词组合,但没有像这样的......

这是一个例子:

apple pear

这应该生成:

苹果

苹果梨
梨苹果

甚至...

apple pear banana

苹果

香蕉
苹果梨
苹果香蕉
梨香蕉
...
...
香蕉 梨 苹果

关键是,以任何顺序使用任何单词零次或一次的所有可能组合。 :)

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...

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. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木槿暧夏七纪年 2024-12-12 19:16:17

底部的最终答案


伪代码(尚未经过测试)

$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;
}

运行代码: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;
}
?>

这会返回所有可能的组合


或者这个看起来更好: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;
}

?>

或者,如果您想查看平键:http://codepad.org/95aNQzXB


最终答案:

这列出了所有内容: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;
}

?>

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;
}

?>
白况 2024-12-12 19:16:17

您可以下载此类: http://pear.php.net/package/Math_Combinatorics

并使用它喜欢:

$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>';
    }
}

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>';
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文