2011-10-19 42 views
0

嘿那裏我已經爲類做了遞歸排列函數,但是我輸出的是不太有利的。 http://codepad.org/DOaMP9oc修復排列輸出

function permute($arr) { 
    $out = array(); 
    if (count($arr) > 1) { 
     $i = 0; 
     foreach($arr as $r => $c) { 
      $n = $arr; 
      unset($n[$r]); 
      $out[$c] = permute($n); 
     } 
    } 
    else 
     return array_shift($arr); 
    return $out; 
} 

如果輸入array(1,2,3,4,5),輸出是:

Array 
(
    [1] => Array 
     (
      [2] => Array 
       (
        [3] => Array 
         (
          [4] => 5 
          [5] => 4 
         ) 

        [4] => Array 
         (
          [3] => 5 
          [5] => 3 
         ) 

        [5] => Array 
         (
          [3] => 4 
          [4] => 3 
         ) 

       ) 
ETC...................... 

這是正確的,你可以這樣key.key.key.key閱讀.value或123451235412435

目前,此輸出爲可讀的格式轉換,我使用這個代碼醜陋塊: http://codepad.org/qyWcRBCl

foreach($out as $k => $a) 
    foreach($a as $l => $b) 
     foreach ($b as $m => $c) 
      foreach ($c as $n => $d) 
       echo $k.$l.$m.$n.$d.'<br>'; 

我怎樣才能改變我的功能,以消除foreach堆棧和輸出類似的格式從permute()

+0

什麼比新行上的每個數字更「可讀」? – Cyclone

+0

foreach方法效率低下,只能設置5個字母。我想從'permute()'中以可讀格式消除foreach和輸出。 –

+2

'如果我的老師很難讀它'。堆棧不是作業答案的地方 –

回答

0

我選擇使用下面的函數,我們可以簡單的方式顯示結果:

function showPerms($a,$i='') { 
    if (is_array($a)) 
     foreach($a as $k => $v) 
      showPerms($v,$i.$k); 
    else 
     echo $i.$a."\n"; 
}     

不過,我仍希望做到這一點一個單一的遞歸函數。

0
function display_permutation($array){ 
if(is_array($array)){ 
    foreach($array as $key => $val){ 
    echo $key; 
display_permutation($val); 
    } 
}else{ 
echo $array; 
} 
} 
+0

我選擇了一個類似的解決方案,唯一的問題是不能準確輸出,它會輸出一個字符串。另外我想結合功能。 –

+0

如果您正在按深度縮進,請嘗試在遞歸方法中包含深度字段,並在每行基礎上回顯選項卡的/大於該部分。 –

+0

http://stackoverflow.com/questions/7827547/fixing-permute-output/7828078#7828078 –

1

我的解決辦法是處理字符串:

function permute($string) { 
    if (strlen($string)<2) { 
     return array($string); 
    } 

    $permutations = array(); 

    // Copy everything but the first character of the string. 
    $copy = substr($string, 1); 

    foreach (permute($copy) as $permutation) { 
     $length = strlen($permutation); 

     // Insert the first character of the original string. 
     for ($i=0; $i<=$length; $i++) { 
      $permutations[] = substr($permutation, 0, $i) . $string[0] . substr($permutation, $i); 
     } 
    } 

    sort($permutations); 

    return $permutations; 
} 

header('Content-type:text/plain'); 
print_r(permute('12345')); 

你已經有一個工作的實施,使我沒有任何疑慮在給那個給你。請注意,該數組不是按順序創建的,因此我只是在最後對其進行排序。還要注意,這隻適用於你打算具有1個字符值的東西,所以做一個汽車名稱的排列是行不通的。


即使你不喜歡這個答案,我建議你使用類型提示數組:

function permute(array $arr) { 

這會強制將傳遞一個數組進去。

+0

不幸的是,賦值是特定於使用數組而不是字符串。 –

+0

謝謝你的PS,我忘記了;) –

0

這裏是我的置換函數和

class Permute { 
    public $results = Array(); 
    public function __construct(array $array) { 
     $this->_permute($array); 
    } 

    private function _permute(array $orig, array $perm = Array()) { 
     if(!count($orig)) { 
      $this->results[] = $perm; 
      return null; 
     } 
     $count = count($orig); 
     for($i = 0; $i < $count; ++$i) { 
      $orig2 = $orig; 
      unset($orig2[$i]); 
      $orig2 = array_values($orig2); 
      $perm2 = $perm; 
      $perm2[] = $orig[$i]; 
      $this->_permute($orig2, $perm2); 
     } 
    } 
} 


$arr = Array(1,2,3,4,5); 
$permute = new Permute($arr); 

foreach($permute->results as $result) { 
    echo join('', $result).'<br>'; 
}