----- Original Message ----- 
From: <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Tuesday, September 11, 2007 8:26 AM
Subject: Re: [php-list] permutations


----- Original Message ----- 
From: "David"

Help! following up on advise I got from an earlier question, I've been
struggling with this for hours. I'm trying to generate a simple list
of unique pairs from a list of 4 items for example, I start with
$names = "david,dianne,phillip,phyllis"

I explode them into an array but  I can't figure out how to
generate the unique list of pairs. I don't want relults like
"david david"

I've tried some recursive stuff but I keep winding up in infinate loops.


Thanks

David
-------------------------------
Hello David,
                   If each item is unique like in your list $names = 
"david,dianne,phillip,phyllis" then you can loop it like a bubble sort.

<code correction>
$names = "david,dianne,phillip,phyllis";
$namearray = explode(",", $names);
$items = (count($namearray) - 1);
// "-1" for zero indexed
$index3 = 0;
for($index1 = 0, $index1 < $items, $index1++)
// "<" so that this loop never reaches the last item
  {
  for($index2 = $index1 + 1, $index2 <= $items, $index2++)
  // form the next item to the last item
    {
    $outarray[$index3] = $namearray[$index1] . " " . $namearray[$index2];
    $index3++;
    }
  }


I have been watching this thread with interest. The problem with this 
approach is that the number of permutations increase factorialy with the 
number of items so you would soon be using very vast tables for the high 
number of permutations.

Thanks, Rob. 




Community email addresses:
  Post message: [email protected]
  Subscribe:    [EMAIL PROTECTED]
  Unsubscribe:  [EMAIL PROTECTED]
  List owner:   [EMAIL PROTECTED]

Shortcut URL to this page:
  http://groups.yahoo.com/group/php-list 
Yahoo! Groups Links





Reply via email to