>_<之前那篇排列組合產生器 Permutations Generator for PHP有BUG啦,那個方法是把字串當作陣列索引去排列組合,再透過索引去重排陣列的元素,熊熊然給他忘了當陣列元素如果11個以上,那麼產生的索引字串就是"012345678910",呃...= =折成陣列變成了array(0,1,2,3,4,5,6,7,8,9,1,0)而不是array(0,1,2,3,4,5,6,7,8,9,10),所以就出錯啦!!

//========================
// usage
//========================
//random pick 2 char from string "ABC",can't repeat pick!
$result = permutations('ABC',2,false);
//random pick 2 char from string "ABC",can repeat pick!
$result = permutations('ABC',2,true);
//random pick 2 element from array('A','B','C'),can't repeat pick!
$result = permutations(array('A','B','C'),2,false);
//random pick 2 element from array('A','B','C'),can repeat pick!
$result = permutations(array('A','B','C'),2,true);
囧rz..自己在某個程式用到才發現,所以我把他整個改寫直接處理陣列元素方法,不過這次我把一堆註解拿掉了@_@,因為寫太多感覺好雜呀~,新的程式碼如下:
/* **************************************************************
 *  Web: http://liaosankai.pixnet.net/blog
 *  Licenses: MIT and GPL
 *  source: http://www.php.happycodings.com/Algorithms/code21.html
 *  modifier: sankai
 *  date:2008-08-24
 * **************************************************************/
function permutations($letters,$num,$repeatPick=true){
    if(is_string($letters)){
        $letters = stringtoarray($letters);
        $return = 'string';
    }
	if(is_array($letters)){
		$result = array();
        $last = array_fill(0,$num,$letters[0]);
		while(count(array_diff($last,array_fill(0,$num,$letters[count($letters)-1]))) != 0){
			if($repeatPick){
                if($return == 'string'){
                    array_push($result,join('',$last));
                } else {
                    array_push($result,$last);
                }
			} else{
				if(count(array_count_values($last)) == $num){
                    if($return == 'string'){
                        array_push($result,join('',$last));
                    } else {
                        array_push($result,$last);
                    }
				}
			}
			$last = char_add($letters,$last,$num-1);
		}
		if($repeatPick){
            if($return == 'string'){
                array_push($result,join('',$last));
            } else {
                array_push($result,$last);
            }
		} else{
			if(count(array_count_values($last)) == $num){
				if($return == 'string'){
                    array_push($result,join('',$last));
                } else {
                    array_push($result,$last);
                }
			}
		}
		return $result;
	} else {
        return array();
    }
}
function char_add($letters,$last,$index){
    if($last[$index] != $letters[count($letters)-1]){
        $last[$index] = $letters[array_search($last[$index],$letters)+1];
        return $last;
    }else{
        $last = changeall($last,$letters[0],$index);
        return char_add($letters,$last,$index-1);
    }
}
function changeall($array,$element,$start = 0,$end = 0){
    if($end == 0) {
        $end = count($array)-1;
    }
    for($i=$start;$i<=$end;$i++){
        $array[$i] = $element;
    }
    return $array;
}
function stringtoarray($string){
	$len = strlen($string);
	for($cur=0; $cur<$len; $cur++){
		$characterarray[] = $string{$cur};
	}
	return $characterarray;
}

低溫烘培 發表在 痞客邦 PIXNET 留言(0) 人氣()