Menu
[B]My pool of letters:
A a B b C c D d E e
[B]What I’ll be doing with them:
making random two letter sets (e.g. Be)
[B]What am I asking you to help me with?
I want to see how many possible combinations there are. And I don’t want the same combination counted twice just because it appeared in a different order (e.g. Be and eB I’d want counted just once). Order does not matter. The combination of letters does.
How could I have PHP do this for me? And if it helps my PHP experience is about 5 stars out of 10. So you don’t have to talk to me like a total novice ?
I don't want the same combination counted twice just because it appeared in a different order[/QUOTE]The order is irrelevant to a combination so two different orders is only one combination even though it is two permutations. Imagine an apple, a pear and an orange in a fruit salad. It doesn't matter how they are arranged the combination would still be the same.
(e.g. Be and eB I'd want counted just once)[/QUOTE]
[code=php]$chars="AaBbCc";
for($i=0;$i<strlen($chars);$i++){
$i_chr = substr($chars,$i,1);
for($j=($i+1);$j<strlen($chars);$j++){
$j_chr = substr($chars,$j,1);
if($j_chr == $i_chr)
continue;
$i=0;
foreach($words as $keys => $vals)
echo $i++.":".$vals."<BR>";
[/code]
Do you just want to know the number of combinations or do you want those combinations returned?[/QUOTE]
My guess is you want[URL=http://en.wikipedia.org/wiki/Permutations_and_combinations#Combination_without_repetition]combinations with repetition[/URL] , is that correct? I just want to be certain of what you want before posting something.[/quote]
nCr .. [/QUOTE]The trouble with that solution is it's not dynamic, which would be nicer. The following is dynamic and fast.
[code=php]<?php
// view the real output
header('Content-Type: text/plain');
// your string
$letters = 'AaBbCcDdEe';
// convert to array
$letters_array = str_2_array($letters);
echo 'The number of two charcter combinations from that string is '.count($result = get_combos($letters_array, 2))."nn";
echo 'The following is the combinations array'."nn";
print_r(array_2d_to_1d($result));
function get_combos($input, $combo_length)
{
$input = array_values($input);
$code = '';
$cnt = count($input);
$ret = array();
$i0 = -1;
for($i=0;$i<$combo_length;++$i)
{
$k = 'i'.($i+1);
$code .= 'for($'.$k.'=$i'.$i.'+1; $'.$k.'< $cnt-'.($combo_length-$i-1).'; ++$'.$k.') ';
}
$code .= '$ret[] = array($input[$i'.implode('], $input[$i',range(1,$combo_length)).']);';
eval($code);
return $ret;
}
function str_2_array($input)
{
for($i = 0, $len = strlen($input); $i < $len; $i++)
{
$rtn[] = $input[$i];
}
return $rtn;
}
function array_2d_to_1d($input)
{
foreach($input as $key => $value)
{
$rtn[$key] = implode($value);
}
return $rtn;
}
?>[/code]
I'm a PHP programmer rank 1.5 our of 10 ? But I'm learning.[/QUOTE]Your biggest trouble will be not crashing the server due to the high number of combinations. For example if an item were to have 10 attributes with 10 choices per attribute that is 10 billion combinations. The idea of php with a default RAM setting of 8 megabytes being able to return such an array and without timing out is fairly remote to say the least.
Your biggest trouble will be not crashing the server due to the high number of combinations. For example if an item were to have 10 attributes with 10 choices per attribute that is 10 billion combinations. The idea of php with a default RAM setting of 8 megabytes being able to return such an array and without timing out is fairly remote to say the least.[/QUOTE]
<i>
</i>for (var i=0;i<sizeof($dataset1);i++) {
for (var j=0;j<sizeof($dataset2);j++) {
for (var k=0;k<sizeof($dataset3);k++) {
for (var l=0;l<sizeof($dataset4);l++) {
echo $dataset1[$i] . $dataset2[$j] . $dataset3[$k] . $dataset4[$l];
}
}
}
}
The trick that I see is that you don't know how many levels to go in.[/QUOTE]If you don't know the number of levels the script needs to be dynamic. This means the script would write the code on the fly (based on the input) and then evaluate it.
[code=php]<?php
$datasets[] = array('a','b','c');
$datasets[] = array('d','e');
$datasets[] = array('f','g');
foreach(datasets($datasets) as $data)
{
echo $data . "<br>n";
}
function datasets($datasets)
{
$part1 = '';
$part2 = '$rtn[] = ';
$i = 0;
foreach(array_keys($datasets) as $key)
{
$part1 .= 'for($k'.$i.'=0; $k'.$i.'<count($datasets['.$key.']); ++$k'.$i.') ';
$part2 .= ($i?'.':'') . '$datasets['.$key.'][$k'.$i++.']';
}
$part2 .= ';';
eval($part1.$part2);
return $rtn;
}
?>[/code]
Hi there. I found this post using Google and its almost what I need.[/QUOTE]
The trouble with that solution is it's not dynamic, which would be nicer. The following is dynamic and fast. [code=php]<?php
// view the real output
header('Content-Type: text/plain');
// your string
$letters = 'AaBbCcDdEe';
// convert to array
$letters_array = str_2_array($letters);
echo 'The number of two charcter combinations from that string is '.count($result = get_combos($letters_array, 2))."nn";
echo 'The following is the combinations array'."nn";
print_r(array_2d_to_1d($result));
function get_combos($input, $combo_length)
{
$input = array_values($input);
$code = '';
$cnt = count($input);
$ret = array();
$i0 = -1;
for($i=0;$i<$combo_length;++$i)
{
$k = 'i'.($i+1);
$code .= 'for($'.$k.'=$i'.$i.'+1; $'.$k.'< $cnt-'.($combo_length-$i-1).'; ++$'.$k.') ';
}
$code .= '$ret[] = array($input[$i'.implode('], $input[$i',range(1,$combo_length)).']);';
eval($code);
return $ret;
}
function str_2_array($input)
{
for($i = 0, $len = strlen($input); $i < $len; $i++)
{
$rtn[] = $input[$i];
}
return $rtn;
}
function array_2d_to_1d($input)
{
foreach($input as $key => $value)
{
$rtn[$key] = implode($value);
}
return $rtn;
}
?>[/code]
[/QUOTE]
I always try to give my posts titles that preview what they're about and I wonder if that is what made this come up in your search.[/QUOTE]It's more likely the search engine realised the quality of the answer rather than the quality of the question.
There's a problem with this. I made this the pool: 12345Let's say I gave you 1 apple, 1 banana, 1 orange, 1 pear, and 1 peach. 2 apples would not be a possible combination, would it? Unless you had a magic wand.
And it gave me this: The number of two charcter combinations from that string is 10 The following is the combinations array Array ( [0] => 12 [1] => 13 [2] => 14 [3] => 15 [4] => 23 [5] => 24 [6] => 25 [7] => 34 [8] => 35 [9] => 45 )
Where's 11 22 33 44 and 55?[/QUOTE]
My guess is you wantI specifically asked this question in the first place before posting any code.[URL=http://en.wikipedia.org/wiki/Permutations_and_combinations#Combination_without_repetition]combinations without repetition[/URL] , is that correct? I just want to be certain of what you want before posting something.[/QUOTE]
I meant not counting the same combination twice if it simply comes up in a different order, e.g. BA and AB.[/QUOTE]That is why I provided the link, so you could understand the difference between [I]with [/I]and [I]without[/I] and the difference between [I]permutation [/I]and [I]combination[/I]. Anyway your suggestion won't work but the following will:
[code=php]<?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]
That is why I provided the link, so you could understand the difference between with and without and the difference between permutation and combination.[/Quote]
gene_combo
color
0.1.9 — BETA 5.6