Skip to content

Instantly share code, notes, and snippets.

@AdamJonR
Last active December 15, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamJonR/5278480 to your computer and use it in GitHub Desktop.
Save AdamJonR/5278480 to your computer and use it in GitHub Desktop.
Iteration of work from specific, hard-coded instance of algorithm to generic function. In this case, the algorithm requested (http://stackoverflow.com/questions/12293870/algorithm-to-get-all-possible-string-combinations-from-array-up-to-certain-lengt) involved creating all possible combinations from an array of values given a minimum and maximum…
// array from which to create combinations
$arr = ['a','b','c','1','2','3'];
// pick specific case of minimum and maximum length of 5 and just get it done
function combinationsOf5_1($arr) {
$results = [];
for ($i = 0; $i < count($arr); $i++) {
$first = $arr[$i];
for ($j = 0; $j < count($arr); $j++) {
$second = $arr[$j];
for ($k = 0; $k < count($arr); $k++) {
$third = $arr[$i];
for ($l = 0; $l < count($arr); $l++) {
$fourth = $arr[$l];
for ($m = 0; $m < count($arr); $m++) {
$fifth = $arr[$m];
$results[] = $first . $second . $third . $fourth . $fifth;
}
}
}
}
}
return $results;
}
// refactor to set up a container for the indexes (select) used to create a round of the combination, generalize the for loop
function combinationsOf5_2($arr) {
$results = [];
$select = [0,0,0,0,0];
$combinations = pow(count($arr),count($select));
for ($i = 0; $i < $combinations; $i++) {
$combination = '';
foreach ($select as $index) {
$combination .= $arr[$index];
}
$results[] = $combination;
if ($select[4] !== 5) {
$select[4]++;
} else {
$select[4] = 0;
if ($select[3] !== 5) {
$select[3]++;
} else {
$select[3] = 0;
if ($select[2] !== 5) {
$select[2]++;
} else {
$select[2] = 0;
if ($select[1] !== 5) {
$select[1]++;
} else {
$select[1] = 0;
if ($select[0] !== 5) {
$select[0]++;
}
}
}
}
}
}
return $results;
}
// generalize more of the code, including what had been a hard-coded implementation of the incrementation mechanism for the combination indexes in select
function combinationsOf5_3($arr) {
$results = [];
$select = [0,0,0,0,0];
$selectCount = count($select);
$arrCount = count($arr);
$combinations = pow($arrCount, $selectCount);
while ($combinations-- > 0) {
$combination = '';
foreach ($select as $index) {
$combination .= $arr[$index];
}
$results[] = $combination;
for ($i = $selectCount - 1; $i >= 0; $i--) {
if ($select[$i] !== ($arrCount - 1)) {
$select[$i]++;
break;
} else {
$select[$i] = 0;
}
}
}
return $results;
}
// reworked naming for consistency and finished generalizations so the function can generate a list of combinations for any length
function combinationsByLenth($arr, $len) {
$combinations = [];
$select = array_fill(0, $len, 0);
$selectCount = count($select);
$arrCount = count($arr);
$possibleCombinations = pow($arrCount, $selectCount);
while ($possibleCombinations-- > 0) {
$combination = '';
foreach ($select as $index) {
$combination .= $arr[$index];
}
$combinations[] = $combination;
for ($i = $selectCount - 1; $i >= 0; $i--) {
if ($select[$i] !== ($arrCount - 1)) {
$select[$i]++;
break;
} else {
$select[$i] = 0;
}
}
}
return $combinations;
}
// created function that handles cycling through and aggregating various combination lengths
function combinationsByMinMax($arr, $min, $max) {
$combinations = [];
for ($i = $min; $i <= $max; $i++) {
$combinations = array_merge($combinations, combinationsByLenth($arr, $i));
}
return $combinations;
}
// example call
print_r(combinationsByMinMax($arr, 1, 5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment