dourong9253 2015-05-28 23:38
浏览 96
已采纳

如何从字符串中获得所需长度的所有排列?

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

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

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

?>

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

  • 写回答

2条回答 默认 最新

  • duannan4486 2015-05-28 23:59
    关注

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

    So what does this code do?

    1. How many permutations are there?

    Pretty simple:

    nl = amount of permutations

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

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

    33 = 27 permutations

    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 Example

    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
    

    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.

    3. Code:

    <?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);
    
    ?>
    

    output:

    Array
    (
        [0] => Array
            (
                [0] => apple
                [1] => apple
                [2] => apple
            )
        //...
        [26] => Array
            (
                [0] => orange
                [1] => orange
                [2] => orange
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比