You are here: Re: Combination or Permutation question! « PHP Programming Language « IT news, forums, messages
Re: Combination or Permutation question!

Posted by Oli Filth on 09/27/71 11:43

kanka wrote:
> Here is my problem,
>
> I have a string like $numbers="12345";
>
> i wanna find out all possible (unique) combinations of each subs.
> e.g. 1 - 2 - 3 - 4 - 5 - 12 - 13 - 14 - 15 - 23 - 24 - ..... - 234 -
> 245 - .... - 12345
> but not 11 - 22 - 21 - 32 - 321....
>
> here is the function that i wrote not returns 13 - 14 - 15 or 24 - 25
>
> $numbers="12345";
> $total=strlen($numbers);
> for ($j=0; $j< $total; $j++) {
> for ($k=0; $k< ($total-$j); $k++) {
> $c[]=substr($numbers, $k, $j+1);
> }
> }

This sound like you want *permutations*, not *combinations*.

You can create permutations of an array with a recursive function,
something like:

function getPermutations($values)
{
$perms = array(null);
foreach ($values as $key => $v)
{

$temp = $values;
unset($temp[$key]);
$newPerms = getPermutations($temp);
foreach ($newPerms as $perm)
{
$perms[] = ($v . $perm);
}
}

return $perms;
}

This is inefficient, but it works.

--
Oli

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация