如何获得具有所需长度的所有排列出一个字符串? [英] How to get all permutations with a desired length out of a string?

查看:141
本文介绍了如何获得具有所需长度的所有排列出一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,我可以通过一个字符串和所需的长度来获得所有排列从字符串的字符长度固定。

I have a function where I can pass a string and a desired length to get all permutations with a fixed length from the characters of the string.

但现在我想的整个单词的排列,例如

But now I want the permutations of entire words, e.g.

$source = "apple,potatoes,orange";

遗憾的是此功能只给了我与字符排列不整的话,我不知道如何修改code,所以我会得到这些置换出上述例子中的数据:

Sadly this function only gives me the permutations with characters and not entire words and I don't know how to modify the code, so I would get these permutations out of the above example data:

apple,apple
apple,potatoes
apple,orange
potatoes,apple
potatoes,potatoes
potatoes,orange
//...

code:

<?php 

$source = 'AaBbCcDdEe'; 

foreach(combos_with_repetition($source, 2) as $combo) { 
    echo "$combo<br>\n"; 
} 

function combos_with_repetition($input, $combo_len = 2) 
{ 
    for($i = 0; $i < $combo_len; ++$i) 
    { 
        @$part1 .= 'for($k'.$i.' = 0, $len = strlen($input); $k'.$i.' < $len; ++$k'.$i.') '; 
        @$part2 .= ($i?'.':'') . '$input[$k'.$i.']'; 
    } 
    eval($part1.'$rtn[] = '.$part2.';'); 
    return $rtn; 
} 

?>

因此​​,任何帮助或提示如何修改code会有所帮助。

So any help or hints how to modify the code would help.

推荐答案

这应该为你工作,即使没有邪恶()

This should work for you and even without evil().

那么,这code呢?

pretty简单:

N =置换量

其中, N 是单词量和每个组合所需的长度。

Where n is the amount of words and l the desired length of each combination.

因此​​,对于这个具体的例子有3个字(苹果 patatoes 橙色),我们希望与3手段的长度每个排列:

So for this specific example there are 3 words (apple, patatoes and orange) and we want each permutation with a length of 3. Means:

3 3 = 27排列

我们通过我们的所有排列,我们已经有循环(一个置换,一个空排列出发( $置换= [[]]; )) ,并为每个排列我们通过我们的数据数组,每个排列每个输入数据合并到一个新的排列。

2. Getting all permutations together

We loop through all our permutations, which we already have(Starting off with one permutation, an "empty permutation" ($permutations = [[]];)), and for each permutation we go through our data array and combine each permutation with each input data to a new permutation.

现在我们这样做,直到我们得到每个排列所需长度。

Now we do this until we get the desired length for each permutation.

2.1示例

Input data:

[1, 2]     //Input array with the data
length = 2 //Desired length for each permutation

为下一次迭代

                               //↓ new permutations for the next iteration
                               │
iteration 0:

    Permutations:
                  - []         │  -> []
                                  │
iteration 1:        ┌─────────────┤
                    │             │
    Permutations:   v             v
                  - []    + 1  │  -> [1]  
                  - []    + 2  │  -> [2]   
                                  │
iteration 2:        ┌─────────────┤
                    │             │
    Permutations:   v             v
                  - []    + 1  │  -> [1]
                  - []    + 2  │  -> [2]
                  - [1]   + 1  │  -> [1,1]  //desired length 2
                  - [1]   + 2  │  -> [1,2]  //desired length 2
                  - [2]   + 1  │  -> [2,1]  //desired length 2 
                  - [2]   + 2  │  -> [2,2]  //desired length 2
                               //↑ All permutations here

所以你可以在上面的例子中看到,我们现在有我们想要的所需长度所有排列,这里2。

So as you can see in the above example we now have all permutations with the desired length which we want, here 2.

但是,为了得到仅具有所需长度,我们将要覆盖的结果阵列中的每个迭代中排列,使在该端部只与预期长度的排列是结果阵列中

But to get only the permutations with the desired length we are overwriting the result array each iteration, so that at the end only the permutations with the expected length are in the results array.

<?php

    function getPermutations($input = [], $length = 2, $delimiter = ",") {
        $permutations = [[]];
        $data = is_array($input) ? $input : explode($delimiter, $input);

        for ($count = 0; $count < $length; $count++) {
            $tmp = [];
            foreach ($permutations as $permutation) {
                foreach ($data as $inputValue)
                    $tmp[] = array_merge($permutation, [$inputValue]);

            }
            $permutations = $tmp;
        }

        return $permutations;

    }


    $result = getPermutations("apple,patatoes,orange", 3);
    print_r($result);

?>

输出:

Array
(
    [0] => Array
        (
            [0] => apple
            [1] => apple
            [2] => apple
        )
    //...
    [26] => Array
        (
            [0] => orange
            [1] => orange
            [2] => orange
        )

)

这篇关于如何获得具有所需长度的所有排列出一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆