Click to See Complete Forum and Search --> : show out all possible order of words of the string
cstan
10-24-2006, 09:22 AM
as title, i need show out all the possible order of word the string.....
can anyone give me some idea???
currently, i was explode the sting to an array, then random the array key to arrange it.
when get the order, i compare it with other, if same then random the key again, else random for other possible order.
when the string was small then still ok to run it but when the string are more that 5 words, it take slow..
if jz 4 word in a string, it jz possible of 24 order only but when got 5 word (120 possible order), it run slow....
can anyone one provide me other idea???? or guide me???
the input are not fix and i need show out the possible order with same number of word as input.
i m using PHP.
thanks.
so_is_this
10-24-2006, 10:46 AM
I don't know if this is the easiest way, but I believe you can think of it in terms of a binary selection of elements from the array. As you count, in binary, from 0 to !n-1 (where n is the number of elements in the array) you first select the elements of the array represented by the 1's in the binary count and reverse them then append the selection of the elements of the array represented by the 0's in the binary count.
EDIT: Hmmm... The idea seemed good on first thought, but I've been desk-checking it and it doesn't seem to be working. Oh, well. ;)
bokeh
10-24-2006, 04:36 PM
For five items the following code is almost immediate. For eight items (40320 permutations) it takes about 2 seconds to run on my server.<?php
header('Content-Type: text/plain');
$words = 'apple banana grape orange peach';
print_r(word_strings($words));
function permutations($end, $start = 0)
{
if($start == $end)
{
return array(array($end));
}
if($start > $end)
{
list($start, $end) = array($end, $start);
}
$rtn = array(array($start));
for($i = $start + 1; $i <= $end; $i++)
{
$temp = array();
foreach($rtn as $k => $v)
{
for($j = 0; $j <= count($v); $j++)
{
$temp[] = array_insert($v, $i, $j);
}
}
$rtn = $temp;
}
return array_reverse($rtn);
}
function array_insert($array, $num, $pos)
{
foreach($array as $k => $v)
{
if($k == $pos)
{
$rtn[] = $num;
}
$rtn[] = $v;
}
if($k < $pos)
{
$rtn[] = $num;
}
return $rtn;
}
function word_strings($in)
{
$words = preg_split('/\s+/', $in);
$perms = permutations(($c = count($words)) - 1);
$code = 'foreach($perms as $v)$rtn[] = implode(" ", array(';
$comma = '';
for($i = 0; $i < $c; $i++)
{
$code .= $comma.'$words[$v['.$i.']]';
$comma =',';
}
$code .= '));';
eval($code);
return $rtn;
}
?>
cstan
10-25-2006, 09:17 AM
For five items the following code is almost immediate. For eight items (40320 permutations) it takes about 2 seconds to run on my server.<?php
header('Content-Type: text/plain');
$words = 'apple banana grape orange peach';
print_r(word_strings($words));
function permutations($end, $start = 0)
{
if($start == $end)
{
return array(array($end));
}
if($start > $end)
{
list($start, $end) = array($end, $start);
}
$rtn = array(array($start));
for($i = $start + 1; $i <= $end; $i++)
{
$temp = array();
foreach($rtn as $k => $v)
{
for($j = 0; $j <= count($v); $j++)
{
$temp[] = array_insert($v, $i, $j);
}
}
$rtn = $temp;
}
return array_reverse($rtn);
}
function array_insert($array, $num, $pos)
{
foreach($array as $k => $v)
{
if($k == $pos)
{
$rtn[] = $num;
}
$rtn[] = $v;
}
if($k < $pos)
{
$rtn[] = $num;
}
return $rtn;
}
function word_strings($in)
{
$words = preg_split('/\s+/', $in);
$perms = permutations(($c = count($words)) - 1);
$code = 'foreach($perms as $v)$rtn[] = implode(" ", array(';
$comma = '';
for($i = 0; $i < $c; $i++)
{
$code .= $comma.'$words[$v['.$i.']]';
$comma =',';
}
$code .= '));';
eval($code);
return $rtn;
}
?>
bokeh-->thanks thanks thanks a lot........